<!DOCTYPE html>
<html lang="ko" dir="ltr">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>React.js 概述 – codthing</title>
    


  
  <script defer src="/js/fuse.min.c4bee9a8d44273d6154fe86006923d4131eb2e8069d8897687137c6f37f553e3.js"></script>



<script src="/js/enquire.min.aa37bdcb743826eecdae5c5d177fc7d6552340f1b4378ffaa9c82b2c6111400b.js"></script>

<script defer src="/js/lazysizes.min.5e11d056075a05065b9c0bfec44084a113fc2976c2952ec804dedb61c7662db9.js"></script>

<script defer src="/js/helper/getParents.min.cd44056d552c69703dced61ae60f99166a0ddf44be93d6aa5d5127d98bf98984.js"></script>

<script defer src="/js/helper/fadeinout.min.58d290b487a552ce9c144bf938c2758c4a3afbc4f17d70d11771c9e3c61fe3d0.js"></script>

<script>
  "use strict";

  window.onload = function() {
    
    var languagedir = JSON.parse("\"ltr\"");
    
    var baseurl = JSON.parse("\"https://codthing.github.io/\"");
    
    var permalink = JSON.parse("\"https://codthing.github.io/react-patterns/render-patterns/overview-react/\"");

    
    var toggleSidebarElem = document.getElementById("toggle-sidebar");
    var toggleMenuElem = document.getElementById("toggle-menu");
    var tocBodyElem = document.querySelector('.toc__body');
    var tocLabelElem = document.querySelector('.toc__label');
    var listMainElem = document.getElementById('list-main');
    var listSideElem = document.getElementById('list-side');
    var singleMenuElem = document.getElementById('single-menu');
    var sliderIcons = document.querySelectorAll('.slider__icon');

    toggleSidebarElem ?
      toggleSidebarElem.addEventListener('change', function (e) {
        if (e.target.checked) {
          if (tocBodyElem) {
            fadeIn(tocBodyElem, 200);
          }
          if (tocLabelElem) {
            fadeIn(tocLabelElem, 200);
          }
          if (listMainElem && listSideElem) {
            listMainElem.className = 'm';
            listSideElem.className = 'r';
          }

          sliderIcons && sliderIcons.forEach(function (elem) {
            if (elem.classList.contains('hide')) {
              elem.classList.remove('hide');
            } else {
              elem.classList.add('hide');
            }
          });

        } else {
          if (tocBodyElem) {
            fadeOut(tocBodyElem, 200);
          }
          if (tocLabelElem) {
            fadeOut(tocLabelElem, 200);
          }
          if (listMainElem && listSideElem) {
            listMainElem.className = 'mr';
            listSideElem.className = 'hide';
          }

          sliderIcons && sliderIcons.forEach(function (elem) {
            if (elem.classList.contains('hide')) {
              elem.classList.remove('hide');
            } else {
              elem.classList.add('hide');
            }
          });
        }
      }) : null;

    toggleMenuElem ?
      toggleMenuElem.addEventListener('change', function (e) {
        if (e.target.checked) {
          if (listMainElem && singleMenuElem) {
            listMainElem.className = 'm';
            singleMenuElem.className = 'l';
          }

          sliderIcons && sliderIcons.forEach(function (elem) {
            if (elem.classList.contains('hide')) {
              elem.classList.remove('hide');
            } else {
              elem.classList.add('hide');
            }
          });

        } else {
          if (listMainElem && singleMenuElem) {
            listMainElem.className = 'lm';
            singleMenuElem.className = 'hide';
          }

          sliderIcons && sliderIcons.forEach(function (elem) {
            if (elem.classList.contains('hide')) {
              elem.classList.remove('hide');
            } else {
              elem.classList.add('hide');
            }
          });
        }
      }) : null;
    


    
    var navCollapseBtn = document.getElementById('navCollapseBtn');
    navCollapseBtn ? navCollapseBtn.addEventListener('click', function(e) {
      var navCollapse = document.querySelector('.navbar__collapse');
      
      if (navCollapse) {
        var dataOpen = navCollapse.getAttribute('data-open');

        if (dataOpen === 'true') {
          navCollapse.setAttribute('data-open', 'false');
          navCollapse.style.maxHeight = 0;
        } else {
          navCollapse.setAttribute('data-open', 'true');
          navCollapse.style.maxHeight = navCollapse.scrollHeight + "px";
        }
      }
    }) : null;
    


    
    var expandBtn = document.querySelectorAll('.expand__button');

    for (let i = 0; i < expandBtn.length; i++) {
      expandBtn[i].addEventListener("click", function () {
        var content = this.nextElementSibling;
        if (content.style.maxHeight) {
          content.style.maxHeight = null;
          this.querySelector('svg').classList.add('expand-icon__right');
          this.querySelector('svg').classList.remove('expand-icon__down');
        } else {
          content.style.maxHeight = content.scrollHeight + "px";
          this.querySelector('svg').classList.remove('expand-icon__right');
          this.querySelector('svg').classList.add('expand-icon__down');
        }
      });
    }
    



    
    document.querySelectorAll('.tab') ? 
    document.querySelectorAll('.tab').forEach(function(elem, idx) {
      var containerId = elem.getAttribute('id');
      var containerElem = elem;
      var tabLinks = elem.querySelectorAll('.tab__link');
      var tabContents = elem.querySelectorAll('.tab__content');
      var ids = [];

      tabLinks && tabLinks.length > 0 ?
      tabLinks.forEach(function(link, index, self) {
        link.onclick = function(e) {
          for (var i = 0; i < self.length; i++) {
            if (index === parseInt(i, 10)) {
              if (!self[i].classList.contains('active')) {
                self[i].classList.add('active');
                tabContents[i].style.display = 'block';
              }
            } else {
              self[i].classList.remove('active');
              tabContents[i].style.display = 'none';
            }
          }
        }
      }) : null;
    }) : null;
    


    
    document.querySelectorAll('.codetab') ? 
    document.querySelectorAll('.codetab').forEach(function(elem, idx) {
      var containerId = elem.getAttribute('id');
      var containerElem = elem;
      var codetabLinks = elem.querySelectorAll('.codetab__link');
      var codetabContents = elem.querySelectorAll('.codetab__content');
      var ids = [];

      codetabLinks && codetabLinks.length > 0 ?
      codetabLinks.forEach(function(link, index, self) {
        link.onclick = function(e) {
          for (var i = 0; i < self.length; i++) {
            if (index === parseInt(i, 10)) {
              if (!self[i].classList.contains('active')) {
                self[i].classList.add('active');
                codetabContents[i].style.display = 'block';
              }
            } else {
              self[i].classList.remove('active');
              codetabContents[i].style.display = 'none';
            }
          }
        }
      }) : null;
    }) : null;
    

    
    
    
    var enableDarkMode = JSON.parse("true");
    var root = document.getElementById('root');
    var toggleToLightBtn = document.getElementById('toggleToLight');
    var toggleToDarkBtn = document.getElementById('toggleToDark');

    if (!enableDarkMode) {
      root.className = 'theme__light';
      localStorage.setItem('theme', 'light');
    }
    
    if (toggleToDarkBtn) {
      toggleToDarkBtn.onclick = function (e) {
        root.className = 'theme__dark';
        localStorage.setItem('theme', 'dark');
        toggleToLightBtn.className = 'navbar__icons--icon';
        toggleToDarkBtn.className = 'hide';
      }
    }

    if (toggleToLightBtn) {
      toggleToLightBtn.onclick = function (e) {
        root.className = 'theme__light';
        localStorage.setItem('theme', 'light');
        toggleToLightBtn.className = 'hide';
        toggleToDarkBtn.className = 'navbar__icons--icon';
      }
    }

    
    document.querySelectorAll('.menu__list').forEach(function(elem) {
      if (elem.classList.contains('active')) {
        elem.style.maxHeight = elem.scrollHeight + "px"; 
      }
    });
    
    document.querySelectorAll('.menu__title--collapse').forEach(function(elem) {
      elem.addEventListener('click', function (e) {
        var content = this.nextElementSibling;
        var menuTitleIcon = this.querySelector('.menu__title--icon');
        if (!content) {
          return null;
        }

        var parent = elem.parentNode;
        while (parent.classList.contains('menu__list') && parent.classList.contains('active')) {
          parent.style.maxHeight = 100 * parent.children.length + "px";
          parent = parent.parentNode;
        }

        if (content.style.maxHeight) {
          content.style.maxHeight = null;
          content.classList.remove('active');
          menuTitleIcon.classList.add('right');
          
          if (languagedir === 'rtl') {
            menuTitleIcon.classList.remove('downrtl');
          } else {
            menuTitleIcon.classList.remove('down');
          }
        } else {
          content.style.maxHeight = content.scrollHeight + "px";
          content.classList.add('active');
          menuTitleIcon.classList.remove('right');
          
          if (languagedir === 'rtl') {
            menuTitleIcon.classList.add('downrtl');
          } else {
            menuTitleIcon.classList.add('down');
          }
        }
      });
    });
    


    
    var mobileLogo = document.getElementById('mobileLogo');
    var modal = document.getElementById("myModal");
    var drawer = document.getElementById('myDrawer');
    var drawerCloseBtn = document.querySelector('.drawer__close');

    var openDrawer = function() {
      modal.style.opacity = 1;

      if (languagedir === 'rtl') {
        modal.style.right = 0;
        drawer.style.right = 0;
      } else {
        modal.style.left = 0;
        drawer.style.left = 0;
      }
    }

    var closeDrawer = function() {
      modal.style.opacity = 0;

      if (languagedir === 'rtl') {
        drawer.style.right = '-100%';
      } else {
        drawer.style.left = '-100%';
      }

      
      setTimeout(function () {
        if (languagedir === 'rtl') {
          modal.style.right = '-100%';
        } else {
          modal.style.left = '-100%';
        }
      }, 250);
    }

    mobileLogo.onclick = function () {
      openDrawer();
      localStorage.setItem('isDrawerOpen', 'true');
    }

    modal.onclick = function () {
      closeDrawer();
      localStorage.setItem('isDrawerOpen', 'false');
    }

    drawerCloseBtn.onclick = function () {
      closeDrawer();
      localStorage.setItem('isDrawerOpen', 'false');
    }
  


  
    var lastScrollTop = window.pageYOffset || document.documentElement.scrollTop;
    var tocElem = document.querySelector('.toc');
    var tableOfContentsElem = tocElem ? tocElem.querySelector('#TableOfContents') : null;
    var singleContentsElem = document.querySelector('.single__contents');
    var dataBGImgs = document.querySelectorAll('div[data-bgimg]');

    
    var tocLevels = JSON.parse("[\"h2\",\"h3\",\"h4\"]");

    if (tocLevels) {
      tocLevels = tocLevels.toString();
    } else {
      tocLevels = "h1, h2, h3, h4, h5, h6";
    }

    
    var isLandingBgImg = JSON.parse("null");
    
    var isHome = JSON.parse("false");

    function setNavbarBG(scrollTop) {
      if (isHome && isLandingBgImg && Object.keys(isLandingBgImg).length) {
        if (isLandingBgImg.height <= scrollTop) {
          dataBGImgs.forEach(function(elem) {
            elem.setAttribute('data-bgimg', 'false');
          });
        } else {
          dataBGImgs.forEach(function (elem) {
            elem.setAttribute('data-bgimg', 'true');
          });
        }
      }
    }
    setNavbarBG(lastScrollTop);

    window.onscroll = function () {
      var st = window.pageYOffset || document.documentElement.scrollTop;
      if (st > lastScrollTop) { 
        singleContentsElem ? 
        singleContentsElem.querySelectorAll(tocLevels.toString()).forEach(function(elem) {
          if (document.documentElement.scrollTop >= elem.offsetTop) {
            if (tableOfContentsElem) {
              var id = elem.getAttribute('id');
              tocElem.querySelectorAll('a').forEach(function (elem) {
                elem.classList.remove('active');
              });
              tocElem.querySelector('a[href="#' + id + '"]') ? 
              tocElem.querySelector('a[href="#' + id + '"]').classList.add('active') : null;
            }
          }
        }) : null;
        setNavbarBG(st);
      } else { 
        singleContentsElem ? 
        singleContentsElem.querySelectorAll(tocLevels.toString()).forEach(function(elem) {
          if (document.documentElement.scrollTop >= elem.offsetTop) {
            if (tableOfContentsElem) {
              var id = elem.getAttribute('id');
              tocElem.querySelectorAll('a').forEach(function (elem) {
                elem.classList.remove('active');
              });
              tocElem.querySelector('a[href="#' + id + '"]') ? 
              tocElem.querySelector('a[href="#' + id + '"]').classList.add('active') : null;
            }
          }
        }) : null;
        setNavbarBG(st);
      }
      lastScrollTop = st <= 0 ? 0 : st;
    };
    



    
    var mobileSearchInputElem = document.querySelector('#search-mobile');
    var mobileSearchClassElem = document.querySelector('.mobile-search');
    var mobileSearchBtnElem = document.querySelector('#mobileSearchBtn');
    var mobileSearchCloseBtnElem = document.querySelector('#search-mobile-close');
    var mobileSearchContainer = document.querySelector('#search-mobile-container');
    var mobileSearchResultsElem = document.querySelector('#search-mobile-results');
    var htmlElem = document.querySelector('html');

    if (mobileSearchClassElem) {
      mobileSearchClassElem.style.display = 'none';
    }

    mobileSearchBtnElem ? 
    mobileSearchBtnElem.addEventListener('click', function () {
      if (mobileSearchContainer) {
        mobileSearchContainer.style.display = 'block';
      }

      if (mobileSearchInputElem) {
        mobileSearchInputElem.focus();
      }

      if (htmlElem) {
        htmlElem.style.overflowY = 'hidden';
      }
    }) : null;

    mobileSearchCloseBtnElem ? 
    mobileSearchCloseBtnElem.addEventListener('click', function() {
      if (mobileSearchContainer) {
        mobileSearchContainer.style.display = 'none';
      }

      if (mobileSearchInputElem) {
        mobileSearchInputElem.value = '';
      }
      
      if (mobileSearchResultsElem) {
        while (mobileSearchResultsElem.firstChild) {
          mobileSearchResultsElem.removeChild(mobileSearchResultsElem.firstChild);
        }
      }

      if (htmlElem) {
        htmlElem.style.overflowY = 'visible';
      }
    }) : null;

    mobileSearchInputElem ? 
    mobileSearchInputElem.addEventListener('keydown', function(e) {
      if (e.key === 'Escape') {
        if (mobileSearchContainer) {
          mobileSearchContainer.style.display = 'none';
        }
        
        if (mobileSearchInputElem) {
          mobileSearchInputElem.value = '';
        }

        if (mobileSearchResultsElem) {
          while (mobileSearchResultsElem.firstChild) {
            mobileSearchResultsElem.removeChild(mobileSearchResultsElem.firstChild);
          }
        }
        if (htmlElem) {
          htmlElem.style.overflowY = 'visible';
        }
      }
    }) : null;
  


  
    var localTheme = localStorage.getItem('theme');
    var rootEleme = document.getElementById('root');
    var selectThemeElem = document.querySelectorAll('.select-theme');
    var selectThemeItemElem = document.querySelectorAll('.select-theme__item');
    
    if (localTheme) {
      selectThemeItemElem ? 
      selectThemeItemElem.forEach(function (elem) {
        if (elem.text.trim() === localTheme) {
          elem.classList.add('is-active');
        } else {
          elem.classList.remove('is-active');
        }
      }) : null;
    }

    selectThemeItemElem ? 
    selectThemeItemElem.forEach(function (v, i) {
      v.addEventListener('click', function (e) {
        var selectedThemeVariant = e.target.text.trim();
        localStorage.setItem('theme', selectedThemeVariant);

        rootEleme.removeAttribute('class');
        rootEleme.classList.add(`theme__${selectedThemeVariant}`);
        selectThemeElem.forEach(function(rootElem) {
          rootElem.querySelectorAll('a').forEach(function (elem) {
            if (elem.classList) {
              if (elem.text.trim() === selectedThemeVariant) {
                if (!elem.classList.contains('is-active')) {
                  elem.classList.add('is-active');
                }
              } else {
                if (elem.classList.contains('is-active')) {
                  elem.classList.remove('is-active');
                }
              }
            }
          });
        });

        if (window.mermaid) {
          if (selectedThemeVariant === "dark" || selectedThemeVariant === "hacker") {
            mermaid.initialize({ theme: 'dark' });
            location.reload();
          } else {
            mermaid.initialize({ theme: 'default' });
            location.reload();
          }
        }

        var utterances = document.querySelector('iframe');
        if (utterances) {
          utterances.contentWindow.postMessage({
            type: 'set-theme',
            theme: selectedThemeVariant === "dark" || selectedThemeVariant === "hacker" ? 'photon-dark' : selectedThemeVariant === 'kimbie' ? 'github-dark-orange' : 'github-light',
          }, 'https://utteranc.es');
        }
      });
    }) : null;
  


  
    
    var langprefix = JSON.parse("\"\"");
    var searchResults = null;
    var searchMenu = null;
    var searchText = null;
    
    
    var enableSearchHighlight = JSON.parse("true");
    
    var enableSearch = JSON.parse("true");

    var fuse = null;

    if (enableSearch) {
      (function initFuse() {
        var xhr = new XMLHttpRequest();
        xhr.open('GET', baseurl + langprefix + "/index.json");
        xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
        xhr.onload = function () {
          if (xhr.status === 200) {
            fuse = new Fuse(JSON.parse(xhr.response.toString('utf-8')), {
              keys: ['title', 'description', 'content'],
              includeMatches: enableSearchHighlight,
              shouldSort: true,
              threshold: 0.4,
              location: 0,
              distance: 100,
              maxPatternLength: 32,
              minMatchCharLength: 1,
            });
          }
          else {
            console.error(`[${xhr.status}]Error:`, xhr.statusText);
          }
        };
        xhr.send();
      })();
    }
    
    function makeLi(ulElem, obj) {
      var li = document.createElement('li');
      li.className = 'search-result__item';
      
      var a = document.createElement('a');
      a.innerHTML = obj.title;
      a.setAttribute('class', 'search-result__item--title');
      a.setAttribute('href', obj.permalink);

      var descDiv = document.createElement('div');
      descDiv.setAttribute('class', 'search-result__item--desc');
      if (obj.description) {
        descDiv.innerHTML = obj.description;
      } else if (obj.content) {
        descDiv.innerHTML = obj.content.substring(0, 80);
      }
      
      li.appendChild(a);
      li.appendChild(descDiv);
      ulElem.appendChild(li);
    }

    function makeHighlightLi(ulElem, obj) {
      var li = document.createElement('li');
      li.className = 'search-result__item';
      var descDiv = null;

      var a = document.createElement('a');
      a.innerHTML = obj.item.title;
      a.setAttribute('class', 'search-result__item--title');
      a.setAttribute('href', obj.item.uri);

      if (obj.matches && obj.matches.length) {
        for (var i = 0; i < obj.matches.length; i++) {
          if ('title' === obj.matches[i].key) {
            a = document.createElement('a');
            a.innerHTML = generateHighlightedText(obj.matches[i].value, obj.matches[i].indices);
            a.setAttribute('class', 'search-result__item--title');
            a.setAttribute('href', obj.item.uri);
          }
          
          if ('description' === obj.matches[i].key) {
            descDiv = document.createElement('div');
            descDiv.setAttribute('class', 'search-result__item--desc');
            descDiv.innerHTML = generateHighlightedText(obj.item.description, obj.matches[i].indices);
          } else if ('content' === obj.matches[i].key) {
            if (!descDiv) {
              descDiv = document.createElement('div');
              descDiv.setAttribute('class', 'search-result__item--desc');
              descDiv.innerHTML = generateHighlightedText(obj.item.content.substring(0, 80), obj.matches[i].indices);
            }
          } else {
            if (obj.item.description) {
              descDiv = document.createElement('div');
              descDiv.setAttribute('class', 'search-result__item--desc');
              descDiv.innerHTML = obj.item.description;
            } else {
              descDiv = document.createElement('div');
              descDiv.setAttribute('class', 'search-result__item--desc');
              descDiv.innerHTML = obj.item.content.substring(0, 80);
            }
          }
        }

        li.appendChild(a);
        if (descDiv) {
          li.appendChild(descDiv);
        }
        if (li) {
          ulElem.appendChild(li);
        }
      }
    }

    function renderSearchResults(searchText, results) {
      searchResults = document.getElementById('search-results');
      searchMenu = document.getElementById('search-menu');
      searchResults.setAttribute('class', 'dd is-active');
      
      var ul = document.createElement('ul');
      ul.setAttribute('class', 'dd-content search-content');

      if (results.length) {
        results.forEach(function (result) {
          var li = document.createElement('li');
          var a = document.createElement('a');
          a.setAttribute('href', result.uri);
          a.setAttribute('class', 'dd-item');
          a.appendChild(li);

          var titleDiv = document.createElement('div');
          titleDiv.innerHTML = result.title;
          titleDiv.setAttribute('class', 'search-result__item--title');

          var descDiv = document.createElement('div');
          descDiv.setAttribute('class', 'search-result__item--desc');
          if (result.description) {
            descDiv.innerHTML = result.description;
          } else if (result.content) {
            descDiv.innerHTML = result.content.substring(0, 80);
          }

          li.appendChild(titleDiv);
          li.appendChild(descDiv);
          ul.appendChild(a);
        });
      } else {
        var li = document.createElement('li');
        li.setAttribute('class', 'dd-item');
        li.innerText = 'No results found';
        ul.appendChild(li);
      }

      while (searchMenu.hasChildNodes()) {
        searchMenu.removeChild(
          searchMenu.lastChild
        );
      }
      
      searchMenu.appendChild(ul);
    }

    function renderSearchHighlightResults(searchText, results) {
      searchResults = document.getElementById('search-results');
      searchMenu = document.getElementById('search-menu');
      searchResults.setAttribute('class', 'dd is-active');

      var ul = document.createElement('ul');
      ul.setAttribute('class', 'dd-content search-content');

      if (results.length) {
        results.forEach(function (result) {
          var li = document.createElement('li');
          var a = document.createElement('a');
          var descDiv = null;

          a.setAttribute('href', result.item.uri);
          a.setAttribute('class', 'dd-item');
          a.appendChild(li);

          var titleDiv = document.createElement('div');
          titleDiv.innerHTML = result.item.title;
          titleDiv.setAttribute('class', 'search-result__item--title');
          
          if (result.matches && result.matches.length) {
            for (var i = 0; i < result.matches.length; i++) {
              if ('title' === result.matches[i].key) {
                titleDiv.innerHTML = generateHighlightedText(result.matches[i].value, result.matches[i].indices);
              }

              if ('description' === result.matches[i].key) {
                descDiv = document.createElement('div');
                descDiv.setAttribute('class', 'search-result__item--desc');
                descDiv.innerHTML = generateHighlightedText(result.item.description, result.matches[i].indices);
              } else if ('content' === result.matches[i].key) {
                if (!descDiv) {
                  descDiv = document.createElement('div');
                  descDiv.setAttribute('class', 'search-result__item--desc');
                  descDiv.innerHTML = generateHighlightedText(result.item.content.substring(0, 80), result.matches[i].indices);
                }
              } else {
                if (result.item.description) {
                  descDiv = document.createElement('div');
                  descDiv.setAttribute('class', 'search-result__item--desc');
                  descDiv.innerHTML = result.item.description;
                } else {
                  descDiv = document.createElement('div');
                  descDiv.setAttribute('class', 'search-result__item--desc');
                  descDiv.innerHTML = result.item.content.substring(0, 80);
                }
              }
            }
            
            li.appendChild(titleDiv);
            if (descDiv) {
              li.appendChild(descDiv);
            }
            ul.appendChild(a);
          }
        });
      } else {
        var li = document.createElement('li');
        li.setAttribute('class', 'dd-item');
        li.innerText = 'No results found';
        ul.appendChild(li);
      }

      while (searchMenu.hasChildNodes()) {
        searchMenu.removeChild(
          searchMenu.lastChild
        );
      }
      searchMenu.appendChild(ul);
    }

    function renderSearchResultsMobile(searchText, results) {
      searchResults = document.getElementById('search-mobile-results');

      var content = document.createElement('div');
      content.setAttribute('class', 'mobile-search__content');

      if (results.length > 0) {
        results.forEach(function (result) {
          var item = document.createElement('a');
          item.setAttribute('href', result.uri);
          item.innerHTML = '<div class="mobile-search__item"><div class="mobile-search__item--title">📄 ' + result.title + '</div><div class="mobile-search__item--desc">' + (result.description ? result.description : result.content) + '</div></div>';
          content.appendChild(item);
        });
      } else {
        var item = document.createElement('span');
        content.appendChild(item);
      }

      let wrap = document.getElementById('search-mobile-results');
      while (wrap.firstChild) {
        wrap.removeChild(wrap.firstChild)
      }
      searchResults.appendChild(content);      
    }

    function renderSearchHighlightResultsMobile(searchText, results) {
      searchResults = document.getElementById('search-mobile-results');

      var ul = document.createElement('div');
      ul.setAttribute('class', 'mobile-search__content');

      if (results.length) {
        results.forEach(function (result) {
          var li = document.createElement('li');
          var a = document.createElement('a');
          var descDiv = null;

          a.setAttribute('href', result.item.uri);
          a.appendChild(li);
          li.setAttribute('class', 'mobile-search__item');

          var titleDiv = document.createElement('div');
          titleDiv.innerHTML = result.item.title;
          titleDiv.setAttribute('class', 'mobile-search__item--title');
          
          if (result.matches && result.matches.length) {
            for (var i = 0; i < result.matches.length; i++) {
              if ('title' === result.matches[i].key) {
                titleDiv.innerHTML = generateHighlightedText(result.matches[i].value, result.matches[i].indices);
              }

              if ('description' === result.matches[i].key) {
                descDiv = document.createElement('div');
                descDiv.setAttribute('class', 'mobile-search__item--desc');
                descDiv.innerHTML = generateHighlightedText(result.item.description, result.matches[i].indices);
              } else if ('content' === result.matches[i].key) {
                if (!descDiv) {
                  descDiv = document.createElement('div');
                  descDiv.setAttribute('class', 'mobile-search__item--desc');
                  descDiv.innerHTML = generateHighlightedText(result.item.content.substring(0, 150), result.matches[i].indices);
                }
              } else {
                if (result.item.description) {
                  descDiv = document.createElement('div');
                  descDiv.setAttribute('class', 'mobile-search__item--desc');
                  descDiv.innerHTML = result.item.description;
                } else {
                  descDiv = document.createElement('div');
                  descDiv.setAttribute('class', 'mobile-search__item--desc');
                  descDiv.innerHTML = result.item.content.substring(0, 150);
                }
              }
            }
            
            li.appendChild(titleDiv);
            if (descDiv) {
              li.appendChild(descDiv);
            }
            ul.appendChild(a);
          }
        });
      } else {
        var item = document.createElement('span');
        ul.appendChild(item);
      }

      let wrap = document.getElementById('search-mobile-results');
      while (wrap.firstChild) {
        wrap.removeChild(wrap.firstChild)
      }
      searchResults.appendChild(ul);
    }

    function generateHighlightedText(text, regions) {
      if (!regions) {
        return text;
      }

      var content = '', nextUnhighlightedRegionStartingIndex = 0;

      regions.forEach(function(region) {
        if (region[0] === region[1]) {
          return null;
        }
        
        content += '' +
          text.substring(nextUnhighlightedRegionStartingIndex, region[0]) +
          '<span class="search__highlight">' +
            text.substring(region[0], region[1] + 1) +
          '</span>' +
        '';
        nextUnhighlightedRegionStartingIndex = region[1] + 1;
      });

      content += text.substring(nextUnhighlightedRegionStartingIndex);

      return content;
    };

    var searchElem = document.getElementById('search');
    var searchMobile = document.getElementById('search-mobile');
    var searchResultsContainer = document.getElementById('search-results');

    searchElem ? 
    searchElem.addEventListener('input', function(e) {
      if (!e.target.value | window.innerWidth < 770) {
        searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
        return null;
      }

      searchText = e.target.value;
      var results = fuse.search(e.target.value);

      if (enableSearchHighlight) {
        renderSearchHighlightResults(searchText, results);
      } else {
        renderSearchResults(searchText, results);
      }

      var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
      dropdownItems ? dropdownItems.forEach(function (item) {
        item.addEventListener('mousedown', function (e) {
          e.target.click();
        });
      }) : null;
    }) : null;

    searchElem ? 
    searchElem.addEventListener('blur', function() {
      if (window.innerWidth < 770) {
        return null;
      }
      searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
    }) : null;

    searchElem ? 
    searchElem.addEventListener('click', function(e) {
      if (window.innerWidth < 770) {
        return null;
      }
      if (!e.target.value) {
        searchResultsContainer ? searchResultsContainer.setAttribute('class', 'dd') : null;
        return null;
      }

      searchText = e.target.value;
      var results = fuse.search(e.target.value);

      if (enableSearchHighlight) {
        renderSearchHighlightResults(searchText, results);
      } else {
        renderSearchResults(searchText, results);
      }

      var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
      dropdownItems ? dropdownItems.forEach(function (item) {
        item.addEventListener('mousedown', function (e) {
          e.target.click();
        });
      }) : null;
    }) : null;

    var searchMenuElem = document.getElementById("search-menu");
    var activeItem = document.querySelector('#search-menu .dd-item.is-active');
    var activeIndex = null;
    var items = null;
    var searchContainerMaxHeight = 350;

    searchElem ? 
    searchElem.addEventListener('keydown', function(e) {
      if (window.innerWidth < 770) {
        return null;
      }

      var items = document.querySelectorAll('#search-menu .dd-item');
      
      if (e.key === 'ArrowDown') {
        if (activeIndex === null) {
          activeIndex = 0;
          items[activeIndex].classList.remove('is-active');
        } else {
          items[activeIndex].classList.remove('is-active');
          activeIndex = activeIndex === items.length - 1 ? 0 : activeIndex + 1;
        }
        items[activeIndex].classList.add('is-active');

        let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
        if (overflowedPixel > 0) {
          document.querySelector(".search-content").scrollTop += items[activeIndex].getBoundingClientRect().height;
        } else if (activeIndex === 0) {
          document.querySelector(".search-content").scrollTop = 0;
        }
      } else if (e.key === 'ArrowUp') {
        if (activeIndex === null) {
          activeIndex = items.length - 1;
          items[activeIndex].classList.remove('is-active');
        } else {
          items[activeIndex].classList.remove('is-active');
          activeIndex = activeIndex === 0 ? items.length - 1 : activeIndex - 1;
        }
        items[activeIndex].classList.add('is-active');
        
        let overflowedPixel = items[activeIndex].offsetTop + items[activeIndex].clientHeight - searchContainerMaxHeight;
        if (overflowedPixel < 0) {
          document.querySelector(".search-content").scrollTop -= items[activeIndex].getBoundingClientRect().height;
        } else {
          document.querySelector(".search-content").scrollTop = overflowedPixel + items[activeIndex].getBoundingClientRect().height;
        }
      } else if (e.key === 'Enter') {
        var currentItemLink = items[activeIndex].getAttribute('href');
        if (currentItemLink) {
          location.href = currentItemLink;
        }
      } else if (e.key === 'Escape') {
        e.target.value = null;
        if (searchResults) {
          searchResults.classList.remove('is-active');
        }
      }
    }) : null;

    searchMobile ? 
    searchMobile.addEventListener('input', function(e) {
      if (!e.target.value) {
        let wrap = document.getElementById('search-mobile-results');
        while (wrap.firstChild) {
          wrap.removeChild(wrap.firstChild);
        }
        return null;
      }

      searchText = e.target.value;
      var results = fuse.search(e.target.value);
      
      if (enableSearchHighlight) {
        renderSearchHighlightResultsMobile(searchText, results);
      } else {
        renderSearchResultsMobile(searchText, results);
      }

      var dropdownItems = searchResultsContainer.querySelectorAll('.dd-item');
      dropdownItems ? dropdownItems.forEach(function (item) {
        item.addEventListener('mousedown', function (e) {
          e.target.click();
        });
      }) : null;
    }) : null;
  
  }
</script>
    


<link rel="stylesheet" href="/css/main.min.css">


    <meta name="description" content="用于构建可重用用户界面组件的 UI 库" />
<meta name="keywords" content="">
<meta name="created" content="2020-01-28T00:34:39&#43;0900">
<meta name="modified" content="2020-01-28T00:34:39&#43;0900">


<meta property="og:site_name" content="codthing">
<meta property="og:title" content="React.js 概述">
<meta property="og:url" content="https://codthing.github.io/react-patterns/render-patterns/overview-react/">
<meta property="og:type" content="article">

<meta name="generator" content="Hugo 0.95.0" />
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="theme-color" content="#ffffff">
<link rel="canonical" href="https://codthing.github.io/react-patterns/render-patterns/overview-react/">
<link rel="manifest" href="/manifest.json">

  <link rel="apple-touch-icon" sizes="57x57" href="/favicon/apple-icon-57x57.png">
  <link rel="apple-touch-icon" sizes="60x60" href="/favicon/apple-icon-60x60.png">
  <link rel="apple-touch-icon" sizes="72x72" href="/favicon/apple-icon-72x72.png">
  <link rel="apple-touch-icon" sizes="76x76" href="/favicon/apple-icon-76x76.png">
  <link rel="apple-touch-icon" sizes="114x114" href="/favicon/apple-icon-114x114.png">
  <link rel="apple-touch-icon" sizes="120x120" href="/favicon/apple-icon-120x120.png">
  <link rel="apple-touch-icon" sizes="144x144" href="/favicon/apple-icon-144x144.png">
  <link rel="apple-touch-icon" sizes="152x152" href="/favicon/apple-icon-152x152.png">
  <link rel="apple-touch-icon" sizes="180x180" href="/favicon/apple-icon-180x180.png">
  <link rel="icon" type="image/png" sizes="192x192" href="/favicon/android-icon-192x192.png">
  <link rel="icon" type="image/png" sizes="192x192" href="/favicon/android-icon-512x512.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/favicon/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="96x96" href="/favicon/favicon-96x96.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/favicon/favicon-16x16.png">
  <meta name="msapplication-TileColor" content="#ffffff">
  <meta name="msapplication-TileImage" content="/ms-icon-144x144.png">

    <script type="application/ld+json">
  {
    "@context": "https://schema.org",
    "@type": "WebPage",
    "headline": "React.js 概述",
    "datePublished": "2020-01-28T00:34:39+09:00",
    "dateModified": "2020-01-28T00:34:39+09:00",
    "url" : "https://codthing.github.io/react-patterns/render-patterns/overview-react/",
    "description": "用于构建可重用用户界面组件的 UI 库",
    "mainEntityOfPage": {
      "@type": "WebPage",
      "@id": "https://codthing.github.io/"
    },
    "publisher": {
      "@type": "Organization",
      "name": "codthing",
      "url": "https://codthing.github.io/"
    }
  }
</script>

    

</head>


<body id="root" class="theme__dark">
    <script>
        var rootElem = document.getElementById('root');
        var themeOptions = JSON.parse("[\"dark\",\"light\"]");
        var localTheme = localStorage.getItem('theme');

        if (localTheme) {
            if (themeOptions && themeOptions.length > 0) {
                themeOptions.includes(localTheme) ? 
                rootElem.className = `theme__${localTheme}` :
                rootElem.className = `theme__${themeOptions[0]}`;
            }
        }
    </script>
    
    <div id="container">
        <div id="myDrawer" class="drawer" style="">
  <div class="drawer__header">
    <a href="/" class="drawer__header--text">
      
    </a>
    <div class="grow"></div>
    <div class="drawer__close">
      <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path fill="currentColor" d="M18.3 5.71c-.39-.39-1.02-.39-1.41 0L12 10.59 7.11 5.7c-.39-.39-1.02-.39-1.41 0-.39.39-.39 1.02 0 1.41L10.59 12 5.7 16.89c-.39.39-.39 1.02 0 1.41.39.39 1.02.39 1.41 0L12 13.41l4.89 4.89c.39.39 1.02.39 1.41 0 .39-.39.39-1.02 0-1.41L13.41 12l4.89-4.89c.38-.38.38-1.02 0-1.4z"/></svg>
    </div>
  </div>

  <div class="drawer__body">
    <div class="menu">
  <h6 class="menu__label">
    
      React.js 概述
    
  </h6>
  <ul>
    
    
      
  
  
  
  
  
  
  

  
  
  
  
  
  <li class="menu__title--collapse " data-depth="0" data-permalink="https://codthing.github.io/react-patterns/design-patterns/">
    <span class="menu__title--collapse-text">
      Design Patterns 设计模式
    </span>
    <span class="menu__title--icon right">
      
        <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24"><path fill="currentColor" d="M9.29 15.88L13.17 12 9.29 8.12c-.39-.39-.39-1.02 0-1.41.39-.39 1.02-.39 1.41 0l4.59 4.59c.39.39.39 1.02 0 1.41L10.7 17.3c-.39.39-1.02.39-1.41 0-.38-.39-.39-1.03 0-1.42z"/></svg>
      
    </span>
  </li>
  <ul class="menu__list " data-data=/design-patterns/ data-link=https://codthing.github.io/react-patterns/render-patterns/overview-react/>
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/design-patterns/" class="menu__title " data-depth="0">设计模式介绍</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/single-patterns/" class="menu__title " data-depth="0">单例模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/proxy-patterns/" class="menu__title " data-depth="0">代理模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/provider-pattern/" class="menu__title " data-depth="0">提供者模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/prototype-pattern/" class="menu__title " data-depth="0">原型模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/presentational-container-pattern/" class="menu__title " data-depth="0">容器/展示模式（X）</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/observer-pattern/" class="menu__title " data-depth="0">观察者模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/module-pattern/" class="menu__title " data-depth="0">模块模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/mixin-pattern/" class="menu__title " data-depth="0">混合模式（X）</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/mediator-pattern/" class="menu__title " data-depth="0">中介者/中间件模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/render-props-pattern/" class="menu__title " data-depth="0">render 道具模式（X）</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/hooks-pattern/" class="menu__title " data-depth="0">Hooks 模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/hoc-pattern/" class="menu__title " data-depth="0">HOC 模式（X）</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/flyweight-pattern/" class="menu__title " data-depth="0">亨元模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/factory-pattern/" class="menu__title " data-depth="0">工厂模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/compound-pattern/" class="menu__title " data-depth="0">复合模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/command-pattern/" class="menu__title " data-depth="0">命令模式</a>
        </li>
      
    
  </ul>
  

    
      
  
  
  
  
  
  
  

  
  
  
  
  
  <li class="menu__title--collapse active" data-depth="0" data-permalink="https://codthing.github.io/react-patterns/render-patterns/">
    <span class="menu__title--collapse-text">
      Render Patterns 渲染模式
    </span>
    <span class="menu__title--icon down">
      
        <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24"><path fill="currentColor" d="M9.29 15.88L13.17 12 9.29 8.12c-.39-.39-.39-1.02 0-1.41.39-.39 1.02-.39 1.41 0l4.59 4.59c.39.39.39 1.02 0 1.41L10.7 17.3c-.39.39-1.02.39-1.41 0-.38-.39-.39-1.03 0-1.42z"/></svg>
      
    </span>
  </li>
  <ul class="menu__list active" data-data=/render-patterns/ data-link=https://codthing.github.io/react-patterns/render-patterns/overview-react/>
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/render-patterns-base/" class="menu__title " data-depth="0">渲染模式介绍</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/overview-react/" class="menu__title active" data-depth="0">React.js 概述</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/overview-next/" class="menu__title " data-depth="0">Next.js 概述</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/client-side-rendering/" class="menu__title " data-depth="0">客户端渲染</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/server-side-rendering/" class="menu__title " data-depth="0">服务端渲染</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/static-rendering/" class="menu__title " data-depth="0">静态生成</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/incremental-static-rendering/" class="menu__title " data-depth="0">增量静态生成 ✔</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/progressive-hydration/" class="menu__title " data-depth="0">渐进补水 ✔</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/streaming-ssr/" class="menu__title " data-depth="0">流(媒体)式服务器端渲染 ✔</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/react-server-components/" class="menu__title " data-depth="0">React 服务器组件 ✔</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/react-selective-hydration/" class="menu__title " data-depth="0">选择性水合作用 ✔</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/islands-architecture/" class="menu__title " data-depth="0">岛屿架构 ✔</a>
        </li>
      
    
  </ul>
  

    
      
  
  
  
  
  
  
  

  
  
  
  
  
  <li class="menu__title--collapse " data-depth="0" data-permalink="https://codthing.github.io/react-patterns/performance-patterns/">
    <span class="menu__title--collapse-text">
      Performance Patterns 性能模式
    </span>
    <span class="menu__title--icon right">
      
        <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24"><path fill="currentColor" d="M9.29 15.88L13.17 12 9.29 8.12c-.39-.39-.39-1.02 0-1.41.39-.39 1.02-.39 1.41 0l4.59 4.59c.39.39.39 1.02 0 1.41L10.7 17.3c-.39.39-1.02.39-1.41 0-.38-.39-.39-1.03 0-1.42z"/></svg>
      
    </span>
  </li>
  <ul class="menu__list " data-data=/performance-patterns/ data-link=https://codthing.github.io/react-patterns/render-patterns/overview-react/>
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/loading-sequence/" class="menu__title " data-depth="0">优化加载顺序</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/static-import/" class="menu__title " data-depth="0">静态导入</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/dynamic-import/" class="menu__title " data-depth="0">动态导入</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/import-on-visibility/" class="menu__title " data-depth="0">导入可见性</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/import-on-interaction/" class="menu__title " data-depth="0">交互导入</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/route-base/" class="menu__title " data-depth="0">基于路由的拆分</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/bundle-splitting/" class="menu__title " data-depth="0">捆绑拆分</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/prpl/" class="menu__title " data-depth="0">PRPL模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/tree-shaking/" class="menu__title " data-depth="0">摇树 tree-shaking</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/preload/" class="menu__title " data-depth="0">预加载</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/prefetch/" class="menu__title " data-depth="0">预取</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/third-party/" class="menu__title " data-depth="0">优化加载第三方</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/virtual-lists/" class="menu__title " data-depth="0">列表虚拟化</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/%E5%8E%8B%E7%BC%A9-javascript/" class="menu__title " data-depth="0">压缩 JavaScript</a>
        </li>
      
    
  </ul>
  

    
  </ul>
</div>



<script>
  var menuTitle = document.querySelectorAll('.menu__title');
  var modal = document.getElementById("myModal");
  var drawer = document.getElementById('myDrawer');

  var closeDrawer = function () {
    setTimeout(function () {
      modal.style.opacity = 0;
      drawer.style.left = '-100%';
      modal.style.left = '-100%';
    }, 250);
  }

  menuTitle ? 
  menuTitle.forEach(function(elem) {
    elem.onclick = function() {
      closeDrawer();
      localStorage.setItem('isDrawerOpen', 'false');
    }
  }) : null;
</script>
  </div>
</div>
<div id="myModal" class="modal" style=""></div>

<script>
  var isDrawerOpen = localStorage.getItem('isDrawerOpen');
  var modal = document.getElementById("myModal");
  var drawer = document.getElementById('myDrawer');
  
  
  var languagedir = JSON.parse("\"ltr\"");

  var openDrawer = function () {
    modal.style.opacity = 1;
    
    if (languagedir === "rtl") {
      modal.style.right = 0;
      drawer.style.right = 0;
    } else {
      modal.style.left = 0;
      drawer.style.left = 0;
    }
  }

  if (isDrawerOpen && isDrawerOpen === 'true') {
    openDrawer();
  }
</script>
        
<nav class="nav">
  <div class="navbar__wrapper" data-bgimg="false">
    <div class="divider">
      <div class="navbar lmr">
        <div id="siteLogo" class="navbar__logo--wrapper">
  
    &nbsp;&nbsp;
  
  
    <a href="/" title="" rel="home" class="navbar__logo--text-link">
      <h6 class="navbar__logo--text" data-bgimg="false"></h6>
    </a>
  
</div>
        
        <div class="navbar__menu--wrapper">
          <div id="tabletLogo" class="hide">
  
    <a href="/" rel="home" class="navbar__logo--link">
      <div class="navbar__icons--icon">
        <svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path fill="currentColor" d="M10 19v-5h4v5c0 .55.45 1 1 1h3c.55 0 1-.45 1-1v-7h1.7c.46 0 .68-.57.33-.87L12.67 3.6c-.38-.34-.96-.34-1.34 0l-8.36 7.53c-.34.3-.13.87.33.87H5v7c0 .55.45 1 1 1h3c.55 0 1-.45 1-1z"/></svg>
      </div>
    </a>
  
</div>
          <div id="mobileLogo" class="hide">
  <a href="#" class="navbar__logo--link">
    <div class="navbar__icons--icon">
      <svg xmlns="http://www.w3.org/2000/svg" width="25" height="25" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path fill="currentColor" d="M4 18h16c.55 0 1-.45 1-1s-.45-1-1-1H4c-.55 0-1 .45-1 1s.45 1 1 1zm0-5h16c.55 0 1-.45 1-1s-.45-1-1-1H4c-.55 0-1 .45-1 1s.45 1 1 1zM3 7c0 .55.45 1 1 1h16c.55 0 1-.45 1-1s-.45-1-1-1H4c-.55 0-1 .45-1 1z"/></svg>
    </div>
  </a>
</div>
          <span id="navMenuMobile" class="hide">
  <button id="navCollapseBtn" class="navbar__dropdown--title" aria-label="Section Menu" data-dir="ltr">
    
      react-patterns
    
    <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"><path fill="currentColor" d="M8.12 9.29L12 13.17l3.88-3.88c.39-.39 1.02-.39 1.41 0 .39.39.39 1.02 0 1.41l-4.59 4.59c-.39.39-1.02.39-1.41 0L6.7 10.7c-.39-.39-.39-1.02 0-1.41.39-.38 1.03-.39 1.42 0z"/></svg>
  </button>
  <div class="navbar__collapse" data-open="false">
  <ul>
    

  
  
  
  
  <li class="navbar__menu--mobile-item ">
    <a href="/javascript" class="">JavaScript</a>
  </li>

  
  
  
  
  <li class="navbar__menu--mobile-item ">
    <a href="/nodejs" class="">Nodejs</a>
  </li>

  
  
  
  
  <li class="navbar__menu--mobile-item ">
    <a href="/react" class="">React</a>
  </li>

  
  
  
  
  <li class="navbar__menu--mobile-item active">
    <a href="/react-patterns" class="active">React-Patterns</a>
  </li>

  
  
  
  
  <li class="navbar__menu--mobile-item ">
    <a href="/react-native" class="">React-Native</a>
  </li>

  
  
  
  
  <li class="navbar__menu--mobile-item ">
    <a href="/rust" class="">Rust</a>
  </li>

  
  
  
  
  <li class="navbar__menu--mobile-item ">
    <a href="/others" class="">Others</a>
  </li>

  </ul>
</div>
</span>
          <ul id="navMenu" class="navbar__menu">
            

  
  
  
  
  <li class="navbar__menu--item ">
    <a href="/javascript" data-bgimg="false">JavaScript</a>
  </li>

  
  
  
  
  <li class="navbar__menu--item ">
    <a href="/nodejs" data-bgimg="false">Nodejs</a>
  </li>

  
  
  
  
  <li class="navbar__menu--item ">
    <a href="/react" data-bgimg="false">React</a>
  </li>

  
  
  
  
  <li class="navbar__menu--item active">
    <a href="/react-patterns" data-bgimg="false">React-Patterns</a>
  </li>

  
  
  
  
  <li class="navbar__menu--item ">
    <a href="/react-native" data-bgimg="false">React-Native</a>
  </li>

  
  
  
  
  <li class="navbar__menu--item ">
    <a href="/rust" data-bgimg="false">Rust</a>
  </li>

  
  
  
  
  <li class="navbar__menu--item ">
    <a href="/others" data-bgimg="false">Others</a>
  </li>

          </ul>
        </div>
        <div class="grow"></div>
        <div id="siteSearch">
          

<div class="search" data-bgimg="false">
  <span class="icon">
    <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="currentColor" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path d="M15.5 14h-.79l-.28-.27c1.2-1.4 1.82-3.31 1.48-5.34-.47-2.78-2.79-5-5.59-5.34-4.23-.52-7.79 3.04-7.27 7.27.34 2.8 2.56 5.12 5.34 5.59 2.03.34 3.94-.28 5.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/></svg>
  </span>
  <input id="search" aria-label="Site Search" class="input" type="text" autocomplete="off" placeholder="Search">
  <div id="search-results" class="dropdown">
    <div id="search-menu" class="dropdown-menu">
    </div>
  </div>
</div>


        </div>
        <div id="mobileSearch" class="hide">
          
<div id="mobileSearchBtn" class="mobile-search__btn">
  <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" fill="currentColor" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path d="M15.5 14h-.79l-.28-.27c1.2-1.4 1.82-3.31 1.48-5.34-.47-2.78-2.79-5-5.59-5.34-4.23-.52-7.79 3.04-7.27 7.27.34 2.8 2.56 5.12 5.34 5.59 2.03.34 3.94-.28 5.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/></svg>
</div>

<div id="search-mobile-container" class="mobile-search hide">
  <div class="mobile-search__top">
    <input id="search-mobile" type="text" aria-label="Mobile Search" placeholder="Search..." class="mobile-search__top--input"/>
    <div id="search-mobile-close" class="mobile-search__top--icon">
      <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24"><path opacity=".87" fill="none" d="M0 0h24v24H0V0z"/><path fill="currentColor" d="M12 2C6.47 2 2 6.47 2 12s4.47 10 10 10 10-4.47 10-10S17.53 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8zm3.59-13L12 10.59 8.41 7 7 8.41 10.59 12 7 15.59 8.41 17 12 13.41 15.59 17 17 15.59 13.41 12 17 8.41z"/></svg>
    </div>
  </div>
  <div id="search-mobile-results" class="mobile-search__body">
    
  </div>
</div>

        </div>
        <div class="navbar__icons">
  
  
<button id="toggleToDark" class="navbar__icons--icon" aria-label="Toggle to dark mode">
  <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none" /><path fill="currentColor" d="M20 15.31L23.31 12 20 8.69V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69zM12 18c-3.31 0-6-2.69-6-6s2.69-6 6-6 6 2.69 6 6-2.69 6-6 6z"/></svg>
</button>
<button id="toggleToLight" class="navbar__icons--icon" aria-label="Toggle to light mode">
  <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"><path d="M0 0h24v24H0z" fill="none"/><path fill="currentColor" d="M20 8.69V4h-4.69L12 .69 8.69 4H4v4.69L.69 12 4 15.31V20h4.69L12 23.31 15.31 20H20v-4.69L23.31 12 20 8.69zM12 18c-3.31 0-6-2.69-6-6s2.69-6 6-6 6 2.69 6 6-2.69 6-6 6zm0-10c-2.21 0-4 1.79-4 4s1.79 4 4 4 4-1.79 4-4-1.79-4-4-4z"/></svg>
</button>
<script>
  var theme = localStorage.getItem('theme');
  var toggleToLightBtn = document.getElementById('toggleToLight');
  var toggleToDarkBtn = document.getElementById('toggleToDark');

  if (theme && theme === 'dark') {
    toggleToLightBtn.className = 'navbar__icons--icon';
    toggleToDarkBtn.className = 'hide';
  } else {
    toggleToLightBtn.className = 'hide';
    toggleToDarkBtn.className = 'navbar__icons--icon';
  }
</script>

  <div class="navbar__icons--icon">
    <a href="https://github.com/codthing" title="Github repository" aria-label="Github repository" target="_blank" rel="noreferrer">
    <svg fill="currentColor" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="22" height="22"><path d="M10.9,2.1c-4.6,0.5-8.3,4.2-8.8,8.7c-0.5,4.7,2.2,8.9,6.3,10.5C8.7,21.4,9,21.2,9,20.8v-1.6c0,0-0.4,0.1-0.9,0.1 c-1.4,0-2-1.2-2.1-1.9c-0.1-0.4-0.3-0.7-0.6-1C5.1,16.3,5,16.3,5,16.2C5,16,5.3,16,5.4,16c0.6,0,1.1,0.7,1.3,1c0.5,0.8,1.1,1,1.4,1 c0.4,0,0.7-0.1,0.9-0.2c0.1-0.7,0.4-1.4,1-1.8c-2.3-0.5-4-1.8-4-4c0-1.1,0.5-2.2,1.2-3C7.1,8.8,7,8.3,7,7.6c0-0.4,0-0.9,0.2-1.3 C7.2,6.1,7.4,6,7.5,6c0,0,0.1,0,0.1,0C8.1,6.1,9.1,6.4,10,7.3C10.6,7.1,11.3,7,12,7s1.4,0.1,2,0.3c0.9-0.9,2-1.2,2.5-1.3 c0,0,0.1,0,0.1,0c0.2,0,0.3,0.1,0.4,0.3C17,6.7,17,7.2,17,7.6c0,0.8-0.1,1.2-0.2,1.4c0.7,0.8,1.2,1.8,1.2,3c0,2.2-1.7,3.5-4,4 c0.6,0.5,1,1.4,1,2.3v2.6c0,0.3,0.3,0.6,0.7,0.5c3.7-1.5,6.3-5.1,6.3-9.3C22,6.1,16.9,1.4,10.9,2.1z"/></svg>
    </a>
  </div>
</div>


      </div>
    </div>
  </div>
</nav>

<script>
  var siteLogo = document.getElementById('siteLogo');
  var tabletLogo = document.getElementById('tabletLogo');
  var mobileLogo = document.getElementById('mobileLogo');
  var navMenu = document.getElementById('navMenu');
  var navMenuMobile = document.getElementById('navMenuMobile');
  var siteSearch = document.getElementById('siteSearch');
  var mobileSearch = document.getElementById('mobileSearch');
  
  enquire.register("screen and (max-width:1280px)", {
    match: function () {
      siteLogo.className = 'navbar__logo--wrapper';
      tabletLogo.className = 'hide';
      mobileLogo.className = 'hide';
      navMenu.className = 'navbar__menu';
      navMenuMobile.className = 'hide';
      siteSearch.className = '';
      mobileSearch.className = 'hide';
    },
    unmatch: function () {
      siteLogo.className = 'navbar__logo--wrapper';
      tabletLogo.className = 'hide';
      mobileLogo.className = 'hide';
      navMenu.className = 'navbar__menu';
      navMenuMobile.className = 'hide';
      siteSearch.className = '';
      mobileSearch.className = 'hide';
    },
  }).register("screen and (max-width:960px)", {
    match: function () {
      siteLogo.className = 'hide';
      tabletLogo.className = '';
      mobileLogo.className = 'hide';
      navMenu.className = 'navbar__menu';
      navMenuMobile.className = 'hide';
      siteSearch.className = 'hide';
      mobileSearch.className = 'navbar__icons--icon';
    },
    unmatch: function () {
      siteLogo.className = 'navbar__logo--wrapper';
      tabletLogo.className = 'hide';
      mobileLogo.className = 'hide';
      navMenu.className = 'navbar__menu';
      navMenuMobile.className = 'hide';
      siteSearch.className = '';
      mobileSearch.className = 'hide';
    },
  }).register("screen and (max-width:600px)", {
    match: function () {
      siteLogo.className = 'hide';
      tabletLogo.className = 'hide';
      mobileLogo.className = '';
      navMenu.className = 'hide';
      navMenuMobile.className = '';
      siteSearch.className = 'hide';
      mobileSearch.className = 'navbar__icons--icon';
    },
    unmatch: function () {
      siteLogo.className = 'hide';
      tabletLogo.className = '';
      mobileLogo.className = 'hide';
      navMenu.className = 'navbar__menu';
      navMenuMobile.className = 'hide';
      siteSearch.className = 'hide';
      mobileSearch.className = 'navbar__icons--icon';
    },
  });
</script>

        
  <div class="top">
  <div class="header__wrapper bgcolor__header">
    <div class="divider">
      <div class="lmr">
        <h2 class="title">React.js 概述</h2>
      </div>
    </div>
  </div>

  <div class="header__wrapper bgcolor__breadcrumb">
    <div class="divider">
      <div class="lmr flexbox">
        
          <nav class="breadcrumb" aria-label="breadcrumbs" data-is-blog="false">
  
  <ol>
    
  
  
  
  
  
  
  
  <li >
    
      <a href="https://codthing.github.io/" class="capitalize">codthing</a>
    
  </li>
  
  
  <li >
    
      <a href="https://codthing.github.io/react-patterns/" class="capitalize">Patterns</a>
    
  </li>
  
  
  <li >
    
      <a href="https://codthing.github.io/react-patterns/render-patterns/" class="capitalize">Render Patterns 渲染模式</a>
    
  </li>
  
  
  <li  class="is-active" >
    
      <span>React.js 概述</span>
    
  </li>
  
  </ol>
  
</nav>
        
        <div class="grow"></div>
        
  <label class="switch switch__rel">
    
      <input id="toggle-sidebar" aria-label="Toggle Sidebar Visibility" type="checkbox" checked />
      <span class="slider">
        <span class="slider__icon ">
          <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path fill="currentColor" d="M5 13h11.17l-4.88 4.88c-.39.39-.39 1.03 0 1.42.39.39 1.02.39 1.41 0l6.59-6.59c.39-.39.39-1.02 0-1.41l-6.58-6.6c-.39-.39-1.02-.39-1.41 0-.39.39-.39 1.02 0 1.41L16.17 11H5c-.55 0-1 .45-1 1s.45 1 1 1z"/></svg>
        </span>
        <span class="slider__icon hide">
          <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path fill="currentColor" d="M19 11H7.83l4.88-4.88c.39-.39.39-1.03 0-1.42-.39-.39-1.02-.39-1.41 0l-6.59 6.59c-.39.39-.39 1.02 0 1.41l6.59 6.59c.39.39 1.02.39 1.41 0 .39-.39.39-1.02 0-1.41L7.83 13H19c.55 0 1-.45 1-1s-.45-1-1-1z"/></svg>
        </span>
      </span>
    
    
  </label>

        
      </div>
    </div>
  </div>
</div>

        
  

<div class="mid">
  <div class="divider">
    
    <nav id="single-menu" class="l" data-dir="ltr">
      
        <div class="menu">
  <h6 class="menu__label">
    
      React.js 概述
    
  </h6>
  <ul>
    
    
      
  
  
  
  
  
  
  

  
  
  
  
  
  <li class="menu__title--collapse " data-depth="0" data-permalink="https://codthing.github.io/react-patterns/design-patterns/">
    <span class="menu__title--collapse-text">
      Design Patterns 设计模式
    </span>
    <span class="menu__title--icon right">
      
        <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24"><path fill="currentColor" d="M9.29 15.88L13.17 12 9.29 8.12c-.39-.39-.39-1.02 0-1.41.39-.39 1.02-.39 1.41 0l4.59 4.59c.39.39.39 1.02 0 1.41L10.7 17.3c-.39.39-1.02.39-1.41 0-.38-.39-.39-1.03 0-1.42z"/></svg>
      
    </span>
  </li>
  <ul class="menu__list " data-data=/design-patterns/ data-link=https://codthing.github.io/react-patterns/render-patterns/overview-react/>
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/design-patterns/" class="menu__title " data-depth="0">设计模式介绍</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/single-patterns/" class="menu__title " data-depth="0">单例模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/proxy-patterns/" class="menu__title " data-depth="0">代理模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/provider-pattern/" class="menu__title " data-depth="0">提供者模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/prototype-pattern/" class="menu__title " data-depth="0">原型模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/presentational-container-pattern/" class="menu__title " data-depth="0">容器/展示模式（X）</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/observer-pattern/" class="menu__title " data-depth="0">观察者模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/module-pattern/" class="menu__title " data-depth="0">模块模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/mixin-pattern/" class="menu__title " data-depth="0">混合模式（X）</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/mediator-pattern/" class="menu__title " data-depth="0">中介者/中间件模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/render-props-pattern/" class="menu__title " data-depth="0">render 道具模式（X）</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/hooks-pattern/" class="menu__title " data-depth="0">Hooks 模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/hoc-pattern/" class="menu__title " data-depth="0">HOC 模式（X）</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/flyweight-pattern/" class="menu__title " data-depth="0">亨元模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/factory-pattern/" class="menu__title " data-depth="0">工厂模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/compound-pattern/" class="menu__title " data-depth="0">复合模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/design-patterns/command-pattern/" class="menu__title " data-depth="0">命令模式</a>
        </li>
      
    
  </ul>
  

    
      
  
  
  
  
  
  
  

  
  
  
  
  
  <li class="menu__title--collapse active" data-depth="0" data-permalink="https://codthing.github.io/react-patterns/render-patterns/">
    <span class="menu__title--collapse-text">
      Render Patterns 渲染模式
    </span>
    <span class="menu__title--icon down">
      
        <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24"><path fill="currentColor" d="M9.29 15.88L13.17 12 9.29 8.12c-.39-.39-.39-1.02 0-1.41.39-.39 1.02-.39 1.41 0l4.59 4.59c.39.39.39 1.02 0 1.41L10.7 17.3c-.39.39-1.02.39-1.41 0-.38-.39-.39-1.03 0-1.42z"/></svg>
      
    </span>
  </li>
  <ul class="menu__list active" data-data=/render-patterns/ data-link=https://codthing.github.io/react-patterns/render-patterns/overview-react/>
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/render-patterns-base/" class="menu__title " data-depth="0">渲染模式介绍</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/overview-react/" class="menu__title active" data-depth="0">React.js 概述</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/overview-next/" class="menu__title " data-depth="0">Next.js 概述</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/client-side-rendering/" class="menu__title " data-depth="0">客户端渲染</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/server-side-rendering/" class="menu__title " data-depth="0">服务端渲染</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/static-rendering/" class="menu__title " data-depth="0">静态生成</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/incremental-static-rendering/" class="menu__title " data-depth="0">增量静态生成 ✔</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/progressive-hydration/" class="menu__title " data-depth="0">渐进补水 ✔</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/streaming-ssr/" class="menu__title " data-depth="0">流(媒体)式服务器端渲染 ✔</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/react-server-components/" class="menu__title " data-depth="0">React 服务器组件 ✔</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/react-selective-hydration/" class="menu__title " data-depth="0">选择性水合作用 ✔</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/render-patterns/islands-architecture/" class="menu__title " data-depth="0">岛屿架构 ✔</a>
        </li>
      
    
  </ul>
  

    
      
  
  
  
  
  
  
  

  
  
  
  
  
  <li class="menu__title--collapse " data-depth="0" data-permalink="https://codthing.github.io/react-patterns/performance-patterns/">
    <span class="menu__title--collapse-text">
      Performance Patterns 性能模式
    </span>
    <span class="menu__title--icon right">
      
        <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24"><path fill="currentColor" d="M9.29 15.88L13.17 12 9.29 8.12c-.39-.39-.39-1.02 0-1.41.39-.39 1.02-.39 1.41 0l4.59 4.59c.39.39.39 1.02 0 1.41L10.7 17.3c-.39.39-1.02.39-1.41 0-.38-.39-.39-1.03 0-1.42z"/></svg>
      
    </span>
  </li>
  <ul class="menu__list " data-data=/performance-patterns/ data-link=https://codthing.github.io/react-patterns/render-patterns/overview-react/>
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/loading-sequence/" class="menu__title " data-depth="0">优化加载顺序</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/static-import/" class="menu__title " data-depth="0">静态导入</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/dynamic-import/" class="menu__title " data-depth="0">动态导入</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/import-on-visibility/" class="menu__title " data-depth="0">导入可见性</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/import-on-interaction/" class="menu__title " data-depth="0">交互导入</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/route-base/" class="menu__title " data-depth="0">基于路由的拆分</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/bundle-splitting/" class="menu__title " data-depth="0">捆绑拆分</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/prpl/" class="menu__title " data-depth="0">PRPL模式</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/tree-shaking/" class="menu__title " data-depth="0">摇树 tree-shaking</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/preload/" class="menu__title " data-depth="0">预加载</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/prefetch/" class="menu__title " data-depth="0">预取</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/third-party/" class="menu__title " data-depth="0">优化加载第三方</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/virtual-lists/" class="menu__title " data-depth="0">列表虚拟化</a>
        </li>
      
    
      
        
        
        <li>
          <a href="https://codthing.github.io/react-patterns/performance-patterns/%E5%8E%8B%E7%BC%A9-javascript/" class="menu__title " data-depth="0">压缩 JavaScript</a>
        </li>
      
    
  </ul>
  

    
  </ul>
</div>


      
    </nav>

    <article id="list-main" class="m" data-dir="ltr">
      <div class="single" data-display="block">
  

  <article class="single__contents" data-dir="ltr">
    <h2 id="react-简介">React 简介</h2>
<p>多年来，对使用 JavaScript 编写用户界面的直接方法的需求不断增加。 <br />
React，也称为 React.js，是 Facebook 设计的开源 JavaScript 库，用于构建用户界面或 UI 组件。</p>
<p>React 当然不是唯一的 UI 库。 <br />
Preact、Vue、Angular、Svelte、Lit 和许多其他工具也非常适合从可重用元素组合界面。<br />
鉴于 React 的受欢迎程度，考虑到我们将使用它来了解本指南中的一些设计、渲染和性能模式，因此有必要了解它的工作原理。</p>
<p>当前端开发人员谈论代码时，通常是在为 Web 设计界面的上下文中。我们认为界面组合的方式是元素，如按钮、列表、导航等。 <br />
React 提供了一种优化和简化的方式来表达这些元素中的接口。它还通过将您的界面组织成<code>三个关键概念——组件、道具和状态</code>，帮助构建复杂和棘手的界面。</p>
<p>因为 React 以组合为中心，所以它可以完美地映射到设计系统的元素。<br />
所以，本质上，为 React 设计实际上是在鼓励你以模块化的方式思考。<br />
它允许您在组合页面或视图之前设计单独的组件，因此可以<code>完全了解每个组件的范围和目的——这个过程称为组件化</code>。</p>
<hr/>
<h2 id="术语">术语</h2>
<ul>
<li>
<p>React / React.js / ReactJS - React 库，由 Facebook 于 2013 年创建</p>
</li>
<li>
<p>ReactDOM - 用于 DOM 和服务器渲染的包</p>
</li>
<li>
<p>JSX - JavaScript 的语法扩展</p>
</li>
<li>
<p>Redux - 集中状态容器</p>
</li>
<li>
<p>Hooks - 一种无需编写类即可使用状态和其他 React 功能的新方法</p>
</li>
<li>
<p>ReactNative - 使用 Javascript 开发跨平台本机应用程序的库</p>
</li>
<li>
<p>Webpack - 在 React 社区中流行的 JavaScript 模块捆绑器。</p>
</li>
<li>
<p>CRA（创建 React 应用程序）- 一种 CLI 工具，用于创建用于引导项目的脚手架 React 应用程序。</p>
</li>
<li>
<p>Next.js - 一个 React 框架，具有许多一流的功能，包括 SSR、代码拆分、性能优化等。</p>
</li>
</ul>
<hr/>
<h2 id="使用-jsx-渲染">使用 JSX 渲染</h2>
<p>我们将在许多示例中使用 JSX。</p>
<p>JSX 是 JavaScript 的扩展，它使用类似 XML 的语法将模板 HTML 嵌入到 JS 中。</p>
<p>它旨在转换为有效的 JavaScript，尽管该转换的语义是特定于实现的。</p>
<p>JSX 在 React 库中越来越受欢迎，但此后也出现了其他实现。</p>
<img src="https://lh3.googleusercontent.com/-Fr8uCVia_YY/YcOZGpN7tWI/AAAAAAAACLg/NHaP_XbJ9MYy6iV0XLVA3xiNaq0gaQhIACNcBGAsYHQ/w669-h252/image.png"/>
<hr/>
<h2 id="components-props-state">Components, Props, State</h2>
<p>组件、道具和状态是 React 中的三个关键概念。</p>
<p>实际上，<code>在 React 中看到或做的所有事情都可以归类为这些关键概念中的至少一个</code>，以下是这些关键概念的快速浏览：</p>
<h4 id="1-components">1. Components</h4>
<img src="https://lh3.googleusercontent.com/-DsDyO79QA8U/YcOZp6keSZI/AAAAAAAACLo/E3W41hhU8iURR5ZJO4tVhkPnW1jszQFMACNcBGAsYHQ/w668-h372/image.png"/>
<p>组件是任何 React 应用程序的构建块。 <br />
它们就像 JavaScript 函数，接受<code>任意输入（Props）</code> 并返回描述应该在屏幕上显示的内容的 React 元素。</p>
<p>首先要理解的是，React 应用程序屏幕上的所有内容都是组件的一部分。 <br />
从本质上讲，React 应用程序只是组件内组件中的组件。 所以开发者不用在 React 中构建页面； 他们构建组件。</p>
<p>组件可让您将 UI 拆分为独立的、可重用的部分。 如果您习惯于设计页面，那么以这种模块化方式思考似乎是一个很大的变化。 <br />
但是如果你使用设计系统或风格指南呢？ 那么这可能不像看起来那么大的范式转变。</p>
<p>定义组件最直接的方式是编写 JavaScript 函数。</p>
<pre tabindex="0"><code class="language-base" data-lang="base">function Badge(props) {
  return &lt;h1&gt;Hello, my name is {props.name}&lt;/h1&gt;;
}
</code></pre><p>这个函数是一个有效的 React 组件，因为它接受带有数据的单个 prop（代表属性）对象参数并返回一个 React 元素。 <br />
此类组件称为“函数组件”，因为它们实际上是 JavaScript 函数。</p>
<img src="https://lh3.googleusercontent.com/-a2fswfbUuac/YcOaSDGQ0eI/AAAAAAAACLw/9os5Pa4ODAcxrr0yQop0kxYrgErYzM5yACNcBGAsYHQ/w655-h391/image.png"/>
<p>除了功能组件，另一种类型的组件是“类组件”。 类组件与函数组件的不同之处在于它是由 ES6 class 定义的，如下所示：</p>
<pre tabindex="0"><code class="language-base" data-lang="base">class Badge extends React.Component {
  render() {
    return &lt;h1&gt;Hello, my name is {this.props.name}&lt;/h1&gt;;
  }
}
</code></pre><p><strong>提取组件</strong></p>
<p>为了说明组件可以拆分为更小的组件这一事实，请考虑以下 Tweet 组件：</p>
<img src="https://lh3.googleusercontent.com/-3GQfftNf1xQ/YcOa5y_pmII/AAAAAAAACL4/WF1aMhLR1uoCWXpwK6afQ4ea_Kq3o7fYgCNcBGAsYHQ/w657-h348/image.png"/>
<p>可以按如下方式实现：</p>
<pre tabindex="0"><code class="language-base" data-lang="base">function Tweet(props) {
  return (
    &lt;div className=&#34;Tweet&#34;&gt;
      &lt;div className=&#34;User&#34;&gt;
        &lt;img className=&#34;Avatar&#34;
          src={props.author.avatarUrl}
          alt={props.author.name}
        /&gt;
        &lt;div className=&#34;User-name&#34;&gt;
          {props.author.name}
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div className=&#34;Tweet-text&#34;&gt;
        {props.text}
      &lt;/div&gt;
      &lt;img className=&#34;Tweet-image&#34;
          src={props.image.imageUrl}
          alt={props.image.description}
        /&gt;
      &lt;div className=&#34;Tweet-date&#34;&gt;
        {formatDate(props.date)}
      &lt;/div&gt;
    &lt;/div&gt;
  );
}
</code></pre><p>这个组件可能有点难以操作，因为它是多么的集群，并且重用它的各个部分也很困难。 <br />
但是，我们仍然可以从中提取一些组件。</p>
<p>我们要做的第一件事是提取头像：</p>
<pre tabindex="0"><code class="language-base" data-lang="base">function Avatar(props) {
    return (
      &lt;img className=&#34;Avatar&#34;
        src={props.user.avatarUrl}
        alt={props.user.name}
      /&gt;
    );
  }
</code></pre><p>Avatar 不需要知道它是在 Comment 中呈现的。 <br />
这就是为什么我们给它的道具一个更通用的名字：用户而不是作者。</p>
<p>现在我们将注释简化一点：</p>
<pre tabindex="0"><code class="language-base" data-lang="base">function Tweet(props) {
  return (
    &lt;div className=&#34;Tweet&#34;&gt;
      &lt;div className=&#34;User&#34;&gt;
        &lt;Avatar user={props.author} /&gt;
        &lt;div className=&#34;User-name&#34;&gt;
          {props.author.name}
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div className=&#34;Tweet-text&#34;&gt;
        {props.text}
      &lt;/div&gt;
      &lt;img className=&#34;Tweet-image&#34;
          src={props.image.imageUrl}
          alt={props.image.description}
        /&gt;
      &lt;div className=&#34;Tweet-date&#34;&gt;
        {formatDate(props.date)}
      &lt;/div&gt;
    &lt;/div&gt;
  );
}
</code></pre><p>接下来我们要做的是一个 User 组件，它在用户名的旁边呈现一个_头像_：</p>
<pre tabindex="0"><code class="language-base" data-lang="base">function User(props) {
  return (
    &lt;div className=&#34;User&#34;&gt;
      &lt;Avatar user={props.user} /&gt;
      &lt;div className=&#34;User-name&#34;&gt;
        {props.user.name}
      &lt;/div&gt;
    &lt;/div&gt;
  );
}
</code></pre><p>现在我们将进一步简化 Tweet：</p>
<pre tabindex="0"><code class="language-base" data-lang="base">function Tweet(props) {
  return (
    &lt;div className=&#34;Tweet&#34;&gt;
      &lt;User user={props.author} /&gt;
      &lt;div className=&#34;Tweet-text&#34;&gt;
        {props.text}
      &lt;/div&gt;
      &lt;img className=&#34;Tweet-image&#34;
          src={props.image.imageUrl}
          alt={props.image.description}
        /&gt;
      &lt;div className=&#34;Tweet-date&#34;&gt;
        {formatDate(props.date)}
      &lt;/div&gt;
    &lt;/div&gt;
  );
}
</code></pre><p>提取组件似乎是一项乏味的工作，但是在为大型应用程序编码时，拥有可重用的组件会使事情变得更容易。 <br />
简化组件时要考虑的一个很好的标准是：<br />
<code>如果 UI 的一部分被多次使用（按钮、面板、头像），或者它本身足够复杂（应用程序、FeedStory、评论），那么它是一个很好的候选者 被提取到一个单独的组件</code>。</p>
<h4 id="2-props">2. Props</h4>
<p>Props 是属性的缩写形式，它们只是指 React 中组件的内部数据。 <br />
它们写在组件调用中并传递到组件中。 <br />
它们还使用与 HTML 属性相同的语法，例如 _ prop=&ldquo;value&rdquo;。</p>
<p>关于道具值得记住的两件事:</p>
<ul>
<li>首先，在构建组件之前确定 prop 的值并将其用作蓝图的一部分。</li>
<li>其次，prop 的值永远不会改变，即 props 一旦被传递到组件中就是只读的。</li>
</ul>
<p>访问 prop 的方式是通过每个组件都可以访问的“this.props”属性来引用它。</p>
<h4 id="3-state">3. State</h4>
<p>State 是一个对象，它包含一些可能在组件的生命周期内发生变化的信息。</p>
<p>这意味着它只是存储在组件 Props 中的数据的当前快照。</p>
<p>数据会随着时间的推移而变化，因此管理数据变化方式的技术变得必要，以确保组件在恰当的时间看起来像工程师希望的那样——这称为<code>状态管理</code>。</p>
 <img src="https://lh3.googleusercontent.com/-hGhp63RxNQ4/YcOc0YzM6mI/AAAAAAAACMA/oIcWh2BSj8w6F78MksGmro6G7xGg2K6RwCNcBGAsYHQ/w629-h354/image.png" />
<p>几乎不可能在不了解状态管理的情况下阅读一段关于 React 的内容。</p>
<p>开发人员喜欢阐述这个主题，但在其核心，状态管理并不像听起来那么复杂。</p>
<p>在 React 中，还可以全局跟踪状态，并且可以根据需要在组件之间共享数据。</p>
<p>本质上，这意味着在 React 应用程序中，在新位置加载数据并不像使用其他技术那样昂贵。 <br />
React 应用程序在保存和加载哪些数据以及何时加载方面更加智能。这为制作以新方式使用数据的界面提供了机会。</p>
<p>想想 React 组件，比如具有自己的数据、逻辑和表示的<code>微应用程序</code>。<br />
每个组件都应该有一个单一的目的。作为一名工程师，您可以决定该目的并完全控制每个组件的行为方式和使用的数据。<br />
您不再受页面其余部分数据的限制。在您的设计中，您可以通过各种方式利用这一点。有机会展示可以改善用户体验或使设计中的区域更具上下文的附加数据。</p>
<p><strong>如何在 React 中添加 State</strong></p>
<p><code>在设计时，包括 State 是您应该放到任务的最后。 使用 props 和 events 将所有东西设计得尽可能无状态要好得多。 这使得组件更易于维护、测试和理解</code>。</p>
<p>添加状态应该通过 Redux 和 MobX 等状态容器或容器/包装器组件来完成。</p>
<p>Redux 是其他反应式框架的流行状态管理系统。 它实现了一个<code>由动作驱动的集中状态机</code>。</p>
<img src="https://lh3.googleusercontent.com/-x-aapCyWvMA/YcOd_n7Nx-I/AAAAAAAACMI/52YG89fkA30mNF38f51McK7ftWF21n1AQCNcBGAsYHQ/w670-h377/image.png"/>
<p>在下面的示例中，状态的位置可以是 LoginContainer 本身。 让我们为此使用 React Hooks（这将在下一节中讨论）：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-js" data-lang="js"><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="kr">const</span> <span class="nx">LoginContainer</span> <span class="o">=</span> <span class="p">()</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="kr">const</span> <span class="p">[</span><span class="nx">username</span><span class="p">,</span> <span class="nx">setUsername</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="s2">&#34;&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">  <span class="kr">const</span> <span class="p">[</span><span class="nx">password</span><span class="p">,</span> <span class="nx">setPassword</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="s2">&#34;&#34;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="kr">const</span> <span class="nx">login</span> <span class="o">=</span> <span class="kr">async</span> <span class="nx">event</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="nx">event</span><span class="p">.</span><span class="nx">preventDefault</span><span class="p">();</span>
</span></span><span class="line"><span class="cl">    <span class="kr">const</span> <span class="nx">response</span> <span class="o">=</span> <span class="kr">await</span> <span class="nx">fetch</span><span class="p">(</span><span class="s2">&#34;/api&#34;</span><span class="p">,</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nx">method</span><span class="o">:</span> <span class="s2">&#34;POST&#34;</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">      <span class="nx">body</span><span class="o">:</span> <span class="nx">JSON</span><span class="p">.</span><span class="nx">stringify</span><span class="p">({</span>
</span></span><span class="line"><span class="cl">        <span class="nx">username</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">        <span class="nx">password</span><span class="p">,</span>
</span></span><span class="line"><span class="cl">      <span class="p">}),</span>
</span></span><span class="line"><span class="cl">    <span class="p">});</span>
</span></span><span class="line"><span class="cl">    <span class="c1">// 在这里我们可以检查 response.status 登录或显示错误
</span></span></span><span class="line"><span class="cl"><span class="c1"></span>  <span class="p">};</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="k">return</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="nx">LoginForm</span> <span class="nx">onSubmit</span><span class="o">=</span><span class="p">{</span><span class="nx">login</span><span class="p">}</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="nx">FormInput</span>
</span></span><span class="line"><span class="cl">        <span class="nx">name</span><span class="o">=</span><span class="s2">&#34;username&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="nx">title</span><span class="o">=</span><span class="s2">&#34;Username&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="nx">onChange</span><span class="o">=</span><span class="p">{</span><span class="nx">event</span> <span class="p">=&gt;</span> <span class="nx">setUsername</span><span class="p">(</span><span class="nx">event</span><span class="p">.</span><span class="nx">currentTarget</span><span class="p">.</span><span class="nx">value</span><span class="p">)}</span>
</span></span><span class="line"><span class="cl">        <span class="nx">value</span><span class="o">=</span><span class="p">{</span><span class="nx">username</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">      <span class="o">/&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="nx">FormPasswordInput</span>
</span></span><span class="line"><span class="cl">        <span class="nx">name</span><span class="o">=</span><span class="s2">&#34;password&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="nx">title</span><span class="o">=</span><span class="s2">&#34;Password&#34;</span>
</span></span><span class="line"><span class="cl">        <span class="nx">onChange</span><span class="o">=</span><span class="p">{</span><span class="nx">event</span> <span class="p">=&gt;</span> <span class="nx">setPassword</span><span class="p">(</span><span class="nx">event</span><span class="p">.</span><span class="nx">currentTarget</span><span class="p">.</span><span class="nx">value</span><span class="p">)}</span>
</span></span><span class="line"><span class="cl">        <span class="nx">value</span><span class="o">=</span><span class="p">{</span><span class="nx">password</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">      <span class="o">/&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="nx">SubmitButton</span><span class="o">&gt;</span><span class="nx">Login</span><span class="o">&lt;</span><span class="err">/SubmitButton&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="err">/LoginForm&gt;</span>
</span></span><span class="line"><span class="cl">  <span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">};</span>
</span></span></code></pre></td></tr></table>
</div>
</div><h4 id="props-vs-state">Props vs State</h4>
<p>道具和状态有时会相互混淆，因为它们非常相似。 以下是它们之间的一些主要区别：</p>
<table>
<thead>
<tr>
<th>Props</th>
<th>State</th>
</tr>
</thead>
<tbody>
<tr>
<td>组件之间的数据保持不变</td>
<td>数据是存储在组件的 Props 中的数据的当前快照。 它在组件的生命周期内发生变化</td>
</tr>
<tr>
<td>数据是只读的</td>
<td>数据可以是异步的</td>
</tr>
<tr>
<td>props中的数据不可修改</td>
<td>可以使用 this.setState 修改 state 中的数据</td>
</tr>
<tr>
<td>props 是传递给组件的东西</td>
<td>State 在组件内管理</td>
</tr>
</tbody>
</table>
<hr/>
<h2 id="react-中的其他概念">React 中的其他概念</h2>
<p>组件、道具和状态是你在 React 中所做的一切的三个关键概念。 但还有其他概念需要学习：</p>
<h4 id="1-生命周期">1. 生命周期</h4>
<p>每个React组件都经过三个阶段：安装、渲染和拆卸。 <br />
在这三个阶段发生的一系列事件可以称为组件的生命周期。</p>
<p>虽然这些事件部分与组件的状态（其内部数据）相关，但生命周期略有不同。 <br />
React 具有根据需要加载和卸载组件的内部代码，并且组件可以存在于该内部代码中的多个使用阶段。</p>
<p>生命周期方法有很多，但最常见的是：</p>
<ul>
<li><code>render()</code> - 这个方法是 React 中类组件中唯一需要的方法，也是最常用的方法。 顾名思义，它负责处理组件到 UI 的渲染，它发生在<code>组件的安装和渲染</code>过程中。</li>
</ul>
<p>创建或删除组件时：</p>
<ul>
<li>
<p><code>componentDidMount()</code> 在组件输出渲染到 DOM 后运行</p>
</li>
<li>
<p>在卸载和销毁组件之前立即调用 <code>componentWillUnmount()</code></p>
</li>
</ul>
<p>当 props 或 state 更新时：</p>
<ul>
<li>
<p>当接收到新的 props 或 state 时， <code>shouldComponentUpdate()</code> 在渲染之前被调用。</p>
</li>
<li>
<p><code>componentDidUpdate()</code> 在更新发生后立即调用。 初始渲染不会调用此方法。</p>
</li>
</ul>
<h4 id="2-hoc">2. HOC</h4>
<p>高阶组件  Higher-order component (HOC) 是 React 中用于重用组件逻辑的高级技术。</p>
<p>意思是高阶组件是一个函数，它接受一个组件并返回一个新组件。</p>
<p>它们是从 React 的组合性质中出现的模式。 组件将 props 转换为 UI，而高阶组件将一个组件转换为另一个组件，它们往往在第三方库中流行。</p>
<h4 id="3-context">3. Context</h4>
<p>在典型的 React 应用程序中，数据通过 props 传递，但这对于应用程序中的许多组件所需的某些类型的 props 来说可能很麻烦。</p>
<p>Context 提供了一种在组件之间共享这些类型的数据的方法，而无需显式地通过每个层次结构级别传递 prop。</p>
<p>意思是上下文，我们可以避免通过中间元素传递道具。</p>
<hr/>
<h2 id="react-hooks">React Hooks</h2>
<p>Hooks 是让你从功能组件“钩入”React 状态和生命周期特性的函数。</p>
<p>它们让您无需编写类即可使用状态和其他 React 功能。</p>
<img src="https://lh3.googleusercontent.com/-UZeFPnVnfXo/YcOiD2sj9-I/AAAAAAAACMQ/WHrdus5Ad2Y_IPX9keD1VBdDQPGyAW8rQCNcBGAsYHQ/w679-h271/image.png"/>
<hr/>
<h2 id="在-react-中思考-">在 React 中思考 ✔</h2>
<p>React 真正令人惊奇的一件事是它如何让您在构建应用程序时思考它们。</p>
<p>使用 React Hooks 构建可搜索产品数据表的思考过程。</p>
<h4 id="第-1-步从-模拟-开始想象一下">第 1 步：从 模拟 开始想象一下</h4>
<p>有一个 JSON API 和一个模拟界面：</p>
<img src="https://lh3.googleusercontent.com/-P5pq-hunykc/YcOioFJTfLI/AAAAAAAACMY/i2cEU3VPorA1TJdoXl6JGB04G2ZmnLJygCNcBGAsYHQ/w730-h435/image.png"/>
<p>JSON API 返回一些如下所示的数据：</p>
<pre tabindex="0"><code class="language-base" data-lang="base">[
  {category: &#34;Entertainment&#34;, retweets: &#34;54&#34;, isLocal: false, text: &#34;Omg. A tweet.&#34;},
  {category: &#34;Entertainment&#34;, retweets: &#34;100&#34;, isLocal: false, text: &#34;Omg. Another.&#34;},
  {category: &#34;Technology&#34;, retweets: &#34;32&#34;, isLocal: false, text: &#34;New ECMAScript features!&#34;},
  {category: &#34;Technology&#34;, retweets: &#34;88&#34;, isLocal: true, text: &#34;Wow, learning React!&#34;}
];
</code></pre><p>提示：像 Excalidraw 这样的免费工具可用于绘制 UI 和组件的<code>高级模拟</code>。</p>
<h4 id="第-2-步将-ui-分解为层次结构组件">第 2 步：将 UI 分解为层次结构组件</h4>
<p>有了<code>模拟</code>后，接下来要做的是在模拟中的每个组件（和子组件）周围绘制框，并<code>命名所有这些框</code>，如下所示。</p>
<p>使用<code>单一职责</code>原则：理想情况下，组件应该具有<code>单一功能</code>。 <br />
如果它最终增长，则应将其分解为更小的子组件。 使用相同的技术来决定是否应该创建新函数或对象。</p>
<img src="https://lh3.googleusercontent.com/-H6jilspWRj0/YcOksn6EqiI/AAAAAAAACMg/ijD1Cv6EKQ0-br8fv4702G0i9TL7pNF3wCNcBGAsYHQ/w663-h395/image.png"/>
<p>上图中看到应用程序中有五个组件。 已经列出了每个组件代表的数据。</p>
<ul>
<li>TweetSearchResults（橙色）：完整组件的容器</li>
<li>SearchBar（蓝色）：用户输入要搜索的内容</li>
<li>TweetList（绿色）：根据用户输入显示和过滤推文</li>
<li>TweetCategory（绿松石色）：显示每个类别的标题</li>
<li>TweetRow（红色）：为每条推文显示一行</li>
</ul>
<p>既然已经确定了模拟中的组件，接下来要做的就是将它们分类到一个<code>层次结构</code>中。</p>
<p>在模拟中的另一个组件中找到的组件应显示为层次结构中的子组件。</p>
<p>像这样：</p>
<pre>
- TweetSearchResults
    - SearchBar
    - TweetList
        - TweetCategory
        - TweetRow
</pre>
<h4 id="第-3-步实现应用程序">第 3 步：实现应用程序。</h4>
<p>在去年之前，最快的方法是构建一个版本，该版本接受数据模型并呈现 UI 但具有零交互性，<br />
但是自从引入 React Hooks 以来，实现应用程序的更简单方法是使用 Hooks，如下所示：</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre tabindex="0" class="chroma"><code><span class="lnt">  1
</span><span class="lnt">  2
</span><span class="lnt">  3
</span><span class="lnt">  4
</span><span class="lnt">  5
</span><span class="lnt">  6
</span><span class="lnt">  7
</span><span class="lnt">  8
</span><span class="lnt">  9
</span><span class="lnt"> 10
</span><span class="lnt"> 11
</span><span class="lnt"> 12
</span><span class="lnt"> 13
</span><span class="lnt"> 14
</span><span class="lnt"> 15
</span><span class="lnt"> 16
</span><span class="lnt"> 17
</span><span class="lnt"> 18
</span><span class="lnt"> 19
</span><span class="lnt"> 20
</span><span class="lnt"> 21
</span><span class="lnt"> 22
</span><span class="lnt"> 23
</span><span class="lnt"> 24
</span><span class="lnt"> 25
</span><span class="lnt"> 26
</span><span class="lnt"> 27
</span><span class="lnt"> 28
</span><span class="lnt"> 29
</span><span class="lnt"> 30
</span><span class="lnt"> 31
</span><span class="lnt"> 32
</span><span class="lnt"> 33
</span><span class="lnt"> 34
</span><span class="lnt"> 35
</span><span class="lnt"> 36
</span><span class="lnt"> 37
</span><span class="lnt"> 38
</span><span class="lnt"> 39
</span><span class="lnt"> 40
</span><span class="lnt"> 41
</span><span class="lnt"> 42
</span><span class="lnt"> 43
</span><span class="lnt"> 44
</span><span class="lnt"> 45
</span><span class="lnt"> 46
</span><span class="lnt"> 47
</span><span class="lnt"> 48
</span><span class="lnt"> 49
</span><span class="lnt"> 50
</span><span class="lnt"> 51
</span><span class="lnt"> 52
</span><span class="lnt"> 53
</span><span class="lnt"> 54
</span><span class="lnt"> 55
</span><span class="lnt"> 56
</span><span class="lnt"> 57
</span><span class="lnt"> 58
</span><span class="lnt"> 59
</span><span class="lnt"> 60
</span><span class="lnt"> 61
</span><span class="lnt"> 62
</span><span class="lnt"> 63
</span><span class="lnt"> 64
</span><span class="lnt"> 65
</span><span class="lnt"> 66
</span><span class="lnt"> 67
</span><span class="lnt"> 68
</span><span class="lnt"> 69
</span><span class="lnt"> 70
</span><span class="lnt"> 71
</span><span class="lnt"> 72
</span><span class="lnt"> 73
</span><span class="lnt"> 74
</span><span class="lnt"> 75
</span><span class="lnt"> 76
</span><span class="lnt"> 77
</span><span class="lnt"> 78
</span><span class="lnt"> 79
</span><span class="lnt"> 80
</span><span class="lnt"> 81
</span><span class="lnt"> 82
</span><span class="lnt"> 83
</span><span class="lnt"> 84
</span><span class="lnt"> 85
</span><span class="lnt"> 86
</span><span class="lnt"> 87
</span><span class="lnt"> 88
</span><span class="lnt"> 89
</span><span class="lnt"> 90
</span><span class="lnt"> 91
</span><span class="lnt"> 92
</span><span class="lnt"> 93
</span><span class="lnt"> 94
</span><span class="lnt"> 95
</span><span class="lnt"> 96
</span><span class="lnt"> 97
</span><span class="lnt"> 98
</span><span class="lnt"> 99
</span><span class="lnt">100
</span><span class="lnt">101
</span><span class="lnt">102
</span><span class="lnt">103
</span><span class="lnt">104
</span><span class="lnt">105
</span></code></pre></td>
<td class="lntd">
<pre tabindex="0" class="chroma"><code class="language-js" data-lang="js"><span class="line"><span class="cl"><span class="c1">// i. TweetSearchResults
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">const</span> <span class="nx">TweetSearchResults</span> <span class="o">=</span> <span class="p">({</span><span class="nx">tweets</span><span class="p">})</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="kr">const</span> <span class="p">[</span><span class="nx">filterText</span><span class="p">,</span> <span class="nx">setFilterText</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">  <span class="kr">const</span> <span class="p">[</span><span class="nx">inThisLocation</span><span class="p">,</span> <span class="nx">setInThisLocation</span><span class="p">]</span> <span class="o">=</span> <span class="nx">useState</span><span class="p">(</span><span class="kc">false</span><span class="p">);</span>
</span></span><span class="line"><span class="cl">  <span class="k">return</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="nx">div</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="nx">SearchBar</span>
</span></span><span class="line"><span class="cl">        <span class="nx">filterText</span><span class="o">=</span><span class="p">{</span><span class="nx">filterText</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="nx">inThisLocation</span><span class="o">=</span><span class="p">{</span><span class="nx">inThisLocation</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="nx">setFilterText</span><span class="o">=</span><span class="p">{</span><span class="nx">setFilterText</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="nx">setInThisLocation</span><span class="o">=</span><span class="p">{</span><span class="nx">setInThisLocation</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">      <span class="o">/&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="nx">TweetList</span>
</span></span><span class="line"><span class="cl">        <span class="nx">tweets</span><span class="o">=</span><span class="p">{</span><span class="nx">tweets</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="nx">filterText</span><span class="o">=</span><span class="p">{</span><span class="nx">filterText</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="nx">inThisLocation</span><span class="o">=</span><span class="p">{</span><span class="nx">inThisLocation</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">      <span class="o">/&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="err">/div&gt;</span>
</span></span><span class="line"><span class="cl">  <span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// ii. SearchBar
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">const</span> <span class="nx">SearchBar</span> <span class="o">=</span> <span class="p">({</span><span class="nx">filterText</span><span class="p">,</span> <span class="nx">inThisLocation</span><span class="p">,</span> <span class="nx">setFilterText</span><span class="p">,</span> <span class="nx">setInThisLocation</span><span class="p">})</span> <span class="p">=&gt;</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl">  <span class="o">&lt;</span><span class="nx">form</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="nx">input</span>
</span></span><span class="line"><span class="cl">      <span class="nx">type</span><span class="o">=</span><span class="s2">&#34;text&#34;</span>
</span></span><span class="line"><span class="cl">      <span class="nx">placeholder</span><span class="o">=</span><span class="s2">&#34;Search...&#34;</span>
</span></span><span class="line"><span class="cl">      <span class="nx">value</span><span class="o">=</span><span class="p">{</span><span class="nx">filterText</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">      <span class="nx">onChange</span><span class="o">=</span><span class="p">{(</span><span class="nx">e</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nx">setFilterText</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">value</span><span class="p">)}</span>
</span></span><span class="line"><span class="cl">    <span class="o">/&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="nx">p</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="nx">label</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl">        <span class="o">&lt;</span><span class="nx">input</span>
</span></span><span class="line"><span class="cl">          <span class="nx">type</span><span class="o">=</span><span class="s2">&#34;checkbox&#34;</span>
</span></span><span class="line"><span class="cl">          <span class="nx">checked</span><span class="o">=</span><span class="p">{</span><span class="nx">inThisLocation</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">          <span class="nx">onChange</span><span class="o">=</span><span class="p">{(</span><span class="nx">e</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="nx">setInThisLocation</span><span class="p">(</span><span class="nx">e</span><span class="p">.</span><span class="nx">target</span><span class="p">.</span><span class="nx">checked</span><span class="p">)}</span>
</span></span><span class="line"><span class="cl">        <span class="o">/&gt;</span>
</span></span><span class="line"><span class="cl">        <span class="p">{</span><span class="s1">&#39; &#39;</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="nx">Only</span> <span class="nx">show</span> <span class="nx">tweets</span> <span class="k">in</span> <span class="nx">your</span> <span class="nx">current</span> <span class="nx">location</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="err">/label&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="err">/p&gt;</span>
</span></span><span class="line"><span class="cl">  <span class="o">&lt;</span><span class="err">/form&gt;</span>
</span></span><span class="line"><span class="cl"><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// iii. TweetList
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">const</span> <span class="nx">TweetList</span> <span class="o">=</span> <span class="p">({</span><span class="nx">tweets</span><span class="p">,</span> <span class="nx">filterText</span><span class="p">,</span> <span class="nx">inThisLocation</span><span class="p">})</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="kr">const</span> <span class="nx">rows</span> <span class="o">=</span> <span class="p">[];</span>
</span></span><span class="line"><span class="cl">  <span class="kd">let</span> <span class="nx">lastCategory</span> <span class="o">=</span> <span class="kc">null</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="nx">tweets</span><span class="p">.</span><span class="nx">forEach</span><span class="p">((</span><span class="nx">tweet</span><span class="p">)</span> <span class="p">=&gt;</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="p">(</span><span class="nx">tweet</span><span class="p">.</span><span class="nx">text</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">().</span><span class="nx">indexOf</span><span class="p">(</span><span class="nx">filterText</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">())</span> <span class="o">===</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="k">return</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="p">(</span><span class="nx">inThisLocation</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="nx">tweet</span><span class="p">.</span><span class="nx">isLocal</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="k">return</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="k">if</span> <span class="p">(</span><span class="nx">tweet</span><span class="p">.</span><span class="nx">category</span> <span class="o">!==</span> <span class="nx">lastCategory</span><span class="p">)</span> <span class="p">{</span>
</span></span><span class="line"><span class="cl">      <span class="nx">rows</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">        <span class="o">&lt;</span><span class="nx">TweetCategory</span>
</span></span><span class="line"><span class="cl">          <span class="nx">category</span><span class="o">=</span><span class="p">{</span><span class="nx">tweet</span><span class="p">.</span><span class="nx">category</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">          <span class="nx">key</span><span class="o">=</span><span class="p">{</span><span class="nx">tweet</span><span class="p">.</span><span class="nx">category</span><span class="p">}</span> <span class="o">/&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="nx">rows</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="nx">TweetRow</span>
</span></span><span class="line"><span class="cl">        <span class="nx">tweet</span><span class="o">=</span><span class="p">{</span><span class="nx">tweet</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">        <span class="nx">key</span><span class="o">=</span><span class="p">{</span><span class="nx">tweet</span><span class="p">.</span><span class="nx">text</span><span class="p">}</span> <span class="o">/&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="p">);</span>
</span></span><span class="line"><span class="cl">    <span class="nx">lastCategory</span> <span class="o">=</span> <span class="nx">tweet</span><span class="p">.</span><span class="nx">category</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">  <span class="p">});</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="k">return</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="nx">table</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="nx">thead</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl">        <span class="o">&lt;</span><span class="nx">tr</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl">          <span class="o">&lt;</span><span class="nx">th</span><span class="o">&gt;</span><span class="nx">Tweet</span> <span class="nx">Text</span><span class="o">&lt;</span><span class="err">/th&gt;</span>
</span></span><span class="line"><span class="cl">          <span class="o">&lt;</span><span class="nx">th</span><span class="o">&gt;</span><span class="nx">Retweets</span><span class="o">&lt;</span><span class="err">/th&gt;</span>
</span></span><span class="line"><span class="cl">        <span class="o">&lt;</span><span class="err">/tr&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="err">/thead&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="nx">tbody</span><span class="o">&gt;</span><span class="p">{</span><span class="nx">rows</span><span class="p">}</span><span class="o">&lt;</span><span class="err">/tbody&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="err">/table&gt;</span>
</span></span><span class="line"><span class="cl">  <span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// iv. TweetCategory
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">const</span> <span class="nx">TweetCategory</span> <span class="o">=</span> <span class="p">({</span><span class="nx">category</span><span class="p">})</span> <span class="p">=&gt;</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl">  <span class="o">&lt;</span><span class="nx">tr</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="nx">th</span> <span class="nx">colSpan</span><span class="o">=</span><span class="s2">&#34;2&#34;</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="p">{</span><span class="nx">category</span><span class="p">}</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="err">/th&gt;</span>
</span></span><span class="line"><span class="cl">  <span class="o">&lt;</span><span class="err">/tr&gt;</span>
</span></span><span class="line"><span class="cl"><span class="p">);</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl"><span class="c1">// v. TweetRow
</span></span></span><span class="line"><span class="cl"><span class="c1"></span><span class="kr">const</span> <span class="nx">TweetRow</span> <span class="o">=</span> <span class="p">({</span><span class="nx">tweet</span><span class="p">})</span> <span class="p">=&gt;</span>  <span class="p">{</span>
</span></span><span class="line"><span class="cl">  <span class="kr">const</span> <span class="nx">color</span> <span class="o">=</span> <span class="nx">tweet</span><span class="p">.</span><span class="nx">isLocal</span> <span class="o">?</span> <span class="s1">&#39;inherit&#39;</span> <span class="o">:</span> <span class="s1">&#39;red&#39;</span><span class="p">;</span>
</span></span><span class="line"><span class="cl">
</span></span><span class="line"><span class="cl">  <span class="k">return</span> <span class="p">(</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="nx">tr</span><span class="o">&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="nx">td</span><span class="o">&gt;&lt;</span><span class="nx">span</span> <span class="nx">style</span><span class="o">=</span><span class="s2">&#34;&#34;</span><span class="o">&gt;</span><span class="p">{</span><span class="nx">tweet</span><span class="p">.</span><span class="nx">text</span><span class="p">}</span><span class="o">&lt;</span><span class="err">/span&gt;&lt;/td&gt;</span>
</span></span><span class="line"><span class="cl">      <span class="o">&lt;</span><span class="nx">td</span><span class="o">&gt;</span><span class="p">{</span><span class="nx">tweet</span><span class="p">.</span><span class="nx">retweets</span><span class="p">}</span><span class="o">&lt;</span><span class="err">/td&gt;</span>
</span></span><span class="line"><span class="cl">    <span class="o">&lt;</span><span class="err">/tr&gt;</span>
</span></span><span class="line"><span class="cl">  <span class="p">);</span>
</span></span><span class="line"><span class="cl"><span class="p">}</span>
</span></span></code></pre></td></tr></table>
</div>
</div><p>最终的实现将是按照前面所述的层次结构一起编写的所有代码：</p>
<pre>
- TweetSearchResults
    - SearchBar
    - TweetList
        - TweetCategory
        - TweetRow
</pre>
<hr/>
<h2 id="开始-react-的方式">开始 React 的方式</h2>
<p>有多种方法可以开始使用 React。</p>
<ul>
<li>
<p>直接在网页上加载：这是设置 React 的最简单方法。 将 React JavaScript 添加到您的页面，作为 npm 依赖项或通过 CDN。</p>
</li>
<li>
<p>使用 create-react-app：create-react-app 是一个旨在快使用 React 的项目，任何需要超出单个页面的 React 应用程序都会发现 create-react-app 非常符合这个需求 容易地。</p>
</li>
<li>
<p>更严肃的生产应用程序应该考虑使用 Next.js，因为它具有更强大的默认值（如代码拆分）。</p>
</li>
<li>
<p>代码沙盒：无需安装即可获得 create-react-app 结构的一种简单方法是访问 <a href="https://codesandbox.io/s" target="_blank">https://codesandbox.io/s</a> 并选择“React”。</p>
</li>
<li>
<p>Codepen：如果对 React 组件进行原型设计并喜欢使用 Codepen 那么也可以。</p>
</li>
</ul>
<hr/>
<h2 id="结论">结论</h2>
<p>React.js 库旨在使构建模块化、可重用用户界面组件的过程变得简单直观。</p>
<p>当阅读一些其他指南时，会发现这个简短的介绍是一个有用的高级概述。</p>
<hr/>
<h2 id="知识点">知识点</h2>
<ul>
<li>React</li>
<li>ReactDOM</li>
<li>JSX</li>
<li>Redux</li>
<li>Hooks</li>
<li>ReactNative</li>
<li>Webpack</li>
<li>CRA</li>
<li>Next.js</li>
</ul>

  </article>
</div>
      
<div class="grow"></div>
<nav class="pagination-single">
  
    
      <a href="https://codthing.github.io/react-patterns/render-patterns/render-patterns-base/" title="渲染模式介绍" class="pagination-single__left">
        <div class="pagination-single__icon">
          <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path fill="currentColor" d="M19 11H7.83l4.88-4.88c.39-.39.39-1.03 0-1.42-.39-.39-1.02-.39-1.41 0l-6.59 6.59c-.39.39-.39 1.02 0 1.41l6.59 6.59c.39.39 1.02.39 1.41 0 .39-.39.39-1.02 0-1.41L7.83 13H19c.55 0 1-.45 1-1s-.45-1-1-1z"/></svg>
        </div>
        <div class="pagination-single__right-title">渲染模式介绍</div>
      </a>
    
    <div class="grow"></div>
    
      <a href="https://codthing.github.io/react-patterns/render-patterns/overview-next/" title="Next.js 概述" class="pagination-single__right">
        <div class="pagination-single__left-title">Next.js 概述</div>
        <div class="pagination-single__icon">
          <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24"><path fill="none" d="M0 0h24v24H0V0z"/><path fill="currentColor" d="M5 13h11.17l-4.88 4.88c-.39.39-.39 1.03 0 1.42.39.39 1.02.39 1.41 0l6.59-6.59c.39-.39.39-1.02 0-1.41l-6.58-6.6c-.39-.39-1.02-.39-1.41 0-.39.39-.39 1.02 0 1.41L16.17 11H5c-.55 0-1 .45-1 1s.45 1 1 1z"/></svg>
        </div>
      </a>
    
  
</nav>


      
  

    </article>
    
    <section id="list-side" class="r" data-dir="ltr">
      
        <div class="toc">
  <h6 class="toc__label">此页面上有什么</h6>
  <div class="toc__body">
    <nav id="TableOfContents">
  <ul>
    <li><a href="#react-简介">React 简介</a></li>
    <li><a href="#术语">术语</a></li>
    <li><a href="#使用-jsx-渲染">使用 JSX 渲染</a></li>
    <li><a href="#components-props-state">Components, Props, State</a>
      <ul>
        <li>
          <ul>
            <li><a href="#1-components">1. Components</a></li>
            <li><a href="#2-props">2. Props</a></li>
            <li><a href="#3-state">3. State</a></li>
            <li><a href="#props-vs-state">Props vs State</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="#react-中的其他概念">React 中的其他概念</a>
      <ul>
        <li>
          <ul>
            <li><a href="#1-生命周期">1. 生命周期</a></li>
            <li><a href="#2-hoc">2. HOC</a></li>
            <li><a href="#3-context">3. Context</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="#react-hooks">React Hooks</a></li>
    <li><a href="#在-react-中思考-">在 React 中思考 ✔</a>
      <ul>
        <li>
          <ul>
            <li><a href="#第-1-步从-模拟-开始想象一下">第 1 步：从 模拟 开始想象一下</a></li>
            <li><a href="#第-2-步将-ui-分解为层次结构组件">第 2 步：将 UI 分解为层次结构组件</a></li>
            <li><a href="#第-3-步实现应用程序">第 3 步：实现应用程序。</a></li>
          </ul>
        </li>
      </ul>
    </li>
    <li><a href="#开始-react-的方式">开始 React 的方式</a></li>
    <li><a href="#结论">结论</a></li>
    <li><a href="#知识点">知识点</a></li>
  </ul>
</nav>
  </div>
</div>
      
    </section>

  </div>
</div>

<script>
  'use strict';
  
  function wrap(el, wrapper) {
    el.parentNode.insertBefore(wrapper, el);
    wrapper.appendChild(el);
  }

  (function () {
    var singleContentsElem = document.querySelector('.single__contents');
    singleContentsElem ? 
    singleContentsElem.querySelectorAll('pre > code').forEach(function(elem) {
      var dataLang = elem.getAttribute('data-lang');
      var dataLangWrapper = document.createElement('div');
      var code = null;
      var codeTitle = null;

      if (dataLang && dataLang.includes(':')) {
        code = dataLang.split(':')[0];
        codeTitle = dataLang.split(':')[1];

        dataLangWrapper.className = 'language-' + code;
        dataLangWrapper.setAttribute('data-lang', codeTitle);

        elem.className = 'language-' + code;
        elem.setAttribute('data-lang', codeTitle);
        elem.setAttribute('id', codeTitle);
      }

      if (!dataLang || codeTitle) {
        wrap(elem.parentNode, dataLangWrapper);
      }

    }) : null;
  })();
  


  
  
  var dollarCodeElem = document.querySelectorAll('div.language-\\$');
  var gtCodeElem = document.querySelectorAll('div.language-\\>');
  
  dollarCodeElem ?
  dollarCodeElem.forEach(function(elem) {
    var lnts = elem.parentNode.parentNode ? elem.parentNode.parentNode.querySelectorAll('.lnt') : null;
    lnts ? 
    lnts.forEach(function(lnt) {
      lnt.innerHTML = '$<br/>';
    }) : null;
  }) : null;

  gtCodeElem ?
  gtCodeElem.forEach(function(elem) {
    var lnts = elem.parentNode.parentNode ? elem.parentNode.parentNode.querySelectorAll('.lnt') : null;
    lnts ? 
    lnts.forEach(function(lnt) {
      lnt.innerHTML = '><br/>';
    }) : null;
  }) : null;
  
</script>




<script defer src="/js/helper/getParents.min.js"></script>

<script defer src="/js/helper/closest.min.js"></script>

<script defer src="/js/helper/prev.min.js"></script>

<script defer src="/js/helper/prop.min.js"></script>

<script defer src="/js/helper/fadeinout.min.js"></script>












<script>
  document.addEventListener('DOMContentLoaded', function () {
    'use strict';
    
    
    var tables = document.querySelectorAll('.single__contents > table');
    for (let i = 0; i < tables.length; i++) {
      var table = tables[i];
      var wrapper = document.createElement('div');
      wrapper.className = 'table-wrapper';
      table.parentElement.replaceChild(wrapper, table);
      wrapper.appendChild(table);
    }
    


    
    
    
    var lib = JSON.parse("null");

    if (lib && lib.includes('mermaid')) {
      var themeVariant = localStorage.getItem('theme') || 'light';

      if (themeVariant === "dark") {
        mermaid.initialize({ theme: 'dark' });
      } else {
        mermaid.initialize({ theme: 'default' });
      }
      
      var mermaids = [];
      [].push.apply(mermaids, document.getElementsByClassName('language-mermaid'));
      mermaids.forEach(function(elem) {
        var elemParentNode = elem.parentNode;

        if (elemParentNode !== document.body) {
          elemParentNode.parentNode.insertBefore(elem, elemParentNode);
          elemParentNode.parentNode.removeChild(elemParentNode);
        }

        var newElemWrapper = document.createElement('div');
        newElemWrapper.classList.add('mermaid');
        newElemWrapper.innerHTML = elem.innerHTML;
        elem.replaceWith(newElemWrapper);
      });
    }
    
    

    
    if (lib && lib.includes('katex')) {
      var mathElements = document.getElementsByClassName('math');
      var options = {
        delimiters: [
          { left: "$$", right: "$$", display: true },
          { left: "\\[", right: "\\]", display: true },
          { left: "$", right: "$", display: false },
          { left: "\\(", right: "\\)", display: false }
        ],
      };

      renderMathInElement(document.querySelector('.single__contents'), options);
    }
    


    
    if (lib && lib.includes('flowchartjs')) {
      
      var options = JSON.parse("{\"arrow-end\":\"block\",\"element-color\":\"black\",\"fill\":\"white\",\"flowstate\":{\"approved\":{\"fill\":\"#58C4A3\",\"font-size\":12,\"no-text\":\"n/a\",\"yes-text\":\"APPROVED\"},\"current\":{\"fill\":\"yellow\",\"font-color\":\"red\",\"font-weight\":\"bold\"},\"future\":{\"fill\":\"#FFFF99\"},\"invalid\":{\"fill\":\"#444444\"},\"past\":{\"fill\":\"#CCCCCC\",\"font-size\":12},\"rejected\":{\"fill\":\"#C45879\",\"font-size\":12,\"no-text\":\"REJECTED\",\"yes-text\":\"n/a\"},\"request\":{\"fill\":\"blue\"}},\"font-color\":\"black\",\"font-size\":14,\"line-color\":\"black\",\"line-length\":50,\"line-width\":3,\"no-text\":\"no\",\"scale\":1,\"symbols\":{\"end\":{\"class\":\"end-element\"},\"start\":{\"element-color\":\"green\",\"fill\":\"yellow\",\"font-color\":\"red\"}},\"text-margin\":10,\"x\":0,\"y\":0,\"yes-text\":\"yes\"}");
      var jsonContent = null;

      var flowchartPrefix = "language-flowchart";
      var index = 0;
      Array.prototype.forEach.call(document.querySelectorAll("[class^=" + flowchartPrefix + "]"), function(x){
          x.style.display = 'none'
          x.parentNode.style.backgroundColor = "transparent"
          jsonContent = x.innerText;

          var node0 = document.createElement('div');
          node0.id = 'flowchart' + index;
          x.parentNode.insertBefore(node0, x);

          var diagram = flowchart.parse(jsonContent);
          diagram.drawSVG("flowchart"+index, options);

          index +=1;
      });      
    }
    


    
    if (lib && lib.includes('chart')) {
      var borderColor = "#666";
      var bgColor = "#ddd";
      var borderWidth = 2;

      Chart.defaults.global.elements.rectangle.borderWidth = borderWidth;
      Chart.defaults.global.elements.rectangle.borderColor = borderColor;
      Chart.defaults.global.elements.rectangle.backgroundColor = bgColor;

      Chart.defaults.global.elements.line.borderWidth = borderWidth;
      Chart.defaults.global.elements.line.borderColor = borderColor;
      Chart.defaults.global.elements.line.backgroundColor = bgColor;

      Chart.defaults.global.elements.point.borderWidth = borderWidth;
      Chart.defaults.global.elements.point.borderColor = borderColor;
      Chart.defaults.global.elements.point.backgroundColor = bgColor;

      var chartPrefix = "language-chart";
      var index = 0;
      var jsonContent = null;

      Array.prototype.forEach.call(document.querySelectorAll("[class^=" + chartPrefix + "]"), function (x) {
        x.style.display = 'none'
        x.parentNode.style.backgroundColor = "transparent"
        jsonContent = x.innerText;
        var node0 = document.createElement('canvas');
        var source = null;
        node0.height = 200;
        node0.style.height = 200;
        node0.id = 'myChart' + index;
        source = JSON.parse(jsonContent);
        x.parentNode.insertBefore(node0, x);
        var ctx = document.getElementById('myChart' + index).getContext('2d');
        var myChart = new Chart(ctx, source);
        index += 1;
      });            
    }
    


    
    if (lib && lib.includes('viz')) {
      var vizPrefix = "language-viz-";
      Array.prototype.forEach.call(document.querySelectorAll("[class^=" + vizPrefix + "]"), function (x) {
        x.style.display = 'none'
        x.parentNode.style.backgroundColor = "transparent"
        var engine;
        x.getAttribute("class").split(" ").forEach(function (cls) {
          if (cls.startsWith(vizPrefix)) {
            engine = cls.substr(vizPrefix.length);
          }
        });
        var viz = new Viz();
        viz.renderSVGElement(x.innerText, { engine: engine })
          .then(function (element) {
            element.style.width = "100%";
            x.parentNode.insertBefore(element, x);
          })
      });
    }
    
    
  });
</script>

<script>
  var listSide = document.getElementById('list-side');
  var listMain = document.getElementById('list-main');
  var singleMenu = document.getElementById('single-menu');
  var switchElem = document.querySelector('.switch');
  var wideViewAsDefault = JSON.parse("false");
  
  enquire.register("screen and (max-width:1280px)", {
    match: function () {
      if (wideViewAsDefault) {
        singleMenu.className = 'l';  
        listMain.className = 'mr';
        listSide.className = 'hide';
      } else {
        singleMenu.className = 'l';
        listMain.className = 'm';
        listSide.className = 'r';
      }
    },
    unmatch: function () {
      if (wideViewAsDefault) {
        singleMenu.className = 'l';
        listMain.className = 'mr';
        listSide.className = 'hide';
      } else {
        listSide.className = 'r';
        listMain.className = 'm';
        singleMenu.className = 'l';
      }
    },
  }).register("screen and (max-width:960px)", {
    match: function () {
      singleMenu.className = 'l';
      listMain.className = 'mr';
      listSide.className = 'hide';
      switchElem.className = 'hide';
    },
    unmatch: function () {
      if (wideViewAsDefault) {
        singleMenu.className = 'l';
        listMain.className = 'mr';
        listSide.className = 'hide';
      } else {
        singleMenu.className = 'l';
        listMain.className = 'm';
        listSide.className = 'r';
      }
      switchElem.className = 'switch';
    },
  }).register("screen and (max-width:600px)", {
    match: function () {
      listSide.className = 'hide';
      listMain.className = 'lmr';
      singleMenu.className = 'hide';
      switchElem.className = 'hide';
    },
    unmatch: function () {
      listSide.className = 'hide';
      listMain.className = 'mr';
      singleMenu.className = 'l';
      switchElem.className = 'hide';
    },
  });
</script>

        
<div class="bot">
  <footer class="footer">
    <div class="divider">
      <div class="lmr">
        
          <p class="caption">
            ©2022, codthing
          </p>
          
        
      </div>
    </div>
  </footer>
</div>

    </div>

</body>

</html>